N
N
N!crO2018-06-30 13:15:55
JavaScript
N!crO, 2018-06-30 13:15:55

How to write a handler?

First option:
class FormContainer extends Component {
  constructor() {
    super()
    this.state = {
      seo_title: ''
    }
    this.handleChange = this.handleChange.bind(this)
  }

  handleChange(event) {
    this.setState({ [event.target.id]: event.target.value })
  }
  
  render() {
    const { seo_title } = this.state
    return (
      <form id="article-form">
        <Input
          text="SEO title"
          label="seo_title"
          type="text"
          id="seo_title"
          value={seo_title}
          handleChange={this.handleChange}
        />
      </form>
    )
  }
}

Second option:
class FormContainer extends Component {
  state = {
    seo_title: ''
  }

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

  render() {
    const { seo_title } = this.state
    return (
      <form id="article-form">
        <Input
          text="SEO title"
          label="seo_title"
          type="text"
          id="seo_title"
          value={seo_title}
          handleChange={this.handleChange}
        />
      </form>
    )
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-06-30
@n1croo

The second option is the unspoken standard in the React community. This uses the experimental babel-plugin-transform-class-properties syntax .
The first option is preferable when you need to make the component inherit, since the functions of the class property are not inherited.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question