简体   繁体   中英

React TypeError:Cannot read property 'text' of undefined

I am working on some project but I really stuck on this problem. Why I get this error like TypeError: Cannot read property 'text' of undefined, when my program update state onChange but when I clicked on submit button I get this mess, Please help me out.

my code snippet:

import React, { Component } from "react";
import { Form, Button, Input, Label } from "semantic-ui-react";

class FormField extends Component {
  constructor() {
    super();
    this.state = {};
  }

  handleChange = event => {
    this.setState({ [event.target.name]: event.target.value });
  };

  handleSubmit(event) {
    event.preventDefault();
    const { text } = this.state;
    console.log(text);
  }

  textToCode() {}

  render() {
    return (
      <Form onSubmit={this.handleSubmit}>
        <Form.Field>
          <Label>Enter Text</Label>
          <Input
            name="text"
            value={this.state.text}
            onChange={this.handleChange}
          />
        </Form.Field>

        <Form.Field>
          <Button
            type="submit"
          >
            Submit
          </Button>
        </Form.Field>

        <Form.Field>
          <input disabled />
        </Form.Field>
      </Form>
    );
  }
}

export default FormField;

handlesubmit isn't being passed properly to the event handler. You can write () => this.handlesubmit() in stead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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