dimanche 20 décembre 2020

React State value call

I want to create a form and save some values. I have this constructor code :

  constructor(props) {
super(props);
this.state = {value: ''};

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);}

and have handleChange function:

  handleChange(event) {
this.setState({value: event.target.value});}

and have handleSubmit function:

handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();}

the handleSubmit function there is error :

Property 'value' does not exist on type '{}'.ts(2339).

How to slove it ?

Here is full code:

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('A name was submitted: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

This is the error enter image description here




Aucun commentaire:

Enregistrer un commentaire