score:5

Accepted answer
function handleInputChange (property) {
  return (e) => {
    this.setState({ [property]: e.target.value })
  }
}

class A extends Component {
  constructor (props) {
    super(props)

    this.handleInputChange = handleInputChange.bind(this)
  }

  render () {
    return (
      <div>
        <input type="text" onChange={this.handleInputChange('text1')} />
      </div>
    )
  }
}

score:0

const handleInputChange = (value, property, _this) =>
  _this.setState(prevState => ({
    [property]: value
  }))

class A extends React.Component {
  constructor(props) {...}

  render() {
    return (
      <div>
        <input
          type="text"
          onChange={e => handleInputChange(e.target.value, 'text1', this)}
        />
      </div>
    )
  }
}

class B extends React.Component {
  constructor(props) {...}

  render() {
    return (
      <div>
        <input
          type="text"
          onChange={e => handleInputChange(e.target.value, 'text2', this)}
        />
      </div>
    )
  }
}

Related Query

More Query from same tag