简体   繁体   English

React(next.js) 给我“handleSubmit 未定义”

[英]React(next.js) is giving me "handleSubmit is not defined"

I used react Class components but know I want to use functions.我使用了 React Class 组件,但知道我想使用函数。 And I have a problem because it keeps telling me that handleSubmit is not defined.我有一个问题,因为它一直告诉我没有定义 handleSubmit。 BUT not in call but like in the beginning of function.但不是在调用中,而是在函数的开头。

This was correct.这是正确的。

 class RegistrationForm extends React.Component {
 ........
      handleSubmit = e => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, values) => {
          if (!err) {
            console.log('Received values of form: ', values);
          }
        });
      };
............
render() {
..........
return{
........
}}

And i changed it to this我把它改成了这个

   const RegistrationForm = () => {
  ..........
    handleSubmit = e => {
      e.preventDefault();
    axioswal
      .post('/api/users', {
        firstname,
        secondname,
        email,
        password,
      })
      .then((data) => {
        if (data.status === 'ok') {
          dispatch({ type: 'fetch' });
          redirectTo('/');
        }
          });
      };
  .....
  return {
  ......
  }

And I am calling it here我在这里称它

 return (
        <Form {...formItemLayout} onSubmit={this.handleSubmit}>
          <Form.Item
          label={

But I don't know what to do to make it work.但我不知道该怎么做才能让它发挥作用。 I will be thankful for any help.我将不胜感激任何帮助。

In Functional component, you don't need to put "this" keyword.在功能组件中,您不需要放置“this”关键字。 Just call the handleSubmit as it is.只需按原样调用 handleSubmit 即可。

<Form {...formItemLayout} onSubmit={handleSubmit}></Form>

put a const in front of your method definition(handleSubmit).在你的方法定义(handleSubmit)前面放一个常量。

class RegistrationForm extends React.Component {
 ........
      const handleSubmit = e => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, values) => {
          if (!err) {
            console.log('Received values of form: ', values);
          }
        });
      };
............
render() {
..........
return{
........
}}

and of course no need to put this in calling method if you've used function component.当然,如果您使用过函数组件,则无需将其放入调用方法中。

In functional components you don't define functions inside the component as properties, so you have to use const .在功能组件中,您不会将组件内部的函数定义为属性,因此您必须使用const

So your code would look like this:所以你的代码看起来像这样:

const RegistrationForm = props => {
  ........

  const handleSubmit = e => {
    e.preventDefault();
    props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
      }
    });
  };

  ..........

  return .........
}}

And then in your return statement you wouldn't use this.handleSubmit , just handleSubmit .然后在你的 return 语句中你不会使用this.handleSubmit ,只是handleSubmit Like this:像这样:

return (
  <Form {...formItemLayout} onSubmit={handleSubmit}>
    <Form.Item
      label={

Also, don't use this.props when referencing props.另外,在引用 props 时不要使用this.props The props get passed to your component as function arguments. props 作为函数参数传递给你的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM