React 子组件的 onChange 事件以更新状态

IT技术 javascript reactjs
2021-04-20 18:01:44

我正在尝试学习如何实现 React 表单(ES6 语法)并将每个字段的 onChange 事件传递给负责更新状态的控制器父组件。这适用于标准的 html 元素,但是我正在尝试一个预装的 Datepicker ( https://www.npmjs.com/package/react-bootstrap-date-picker ) 用于日期字段并且不能轻易传递事件以相同的方式备份到父级。有没有简单的方法来解决这个问题?

控制器组件

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

    setJobState(event) {
        var field = event.target.name;
        var value = event.target.value;
        this.state.job[field] = value;
        this.setState({job: this.state.job});
    }


    render () {
        return <Child onChange={this.setJobState.bind(this)} />
    }
}

子组件

class Child extends React.Component {
    constructor (props) {
        super(props);

    }

    render () {
        <form>
         <input type="text" name="jobNumber" onChange={this.props.onChange} /> 
         <DatePicker name="dateCmmenced" onChange={this.props.onChange}  />
        </form>
    }
}
2个回答

onChange处理程序DatePicker不是使用标准浏览器change事件调用的,而是使用valueformattedValue作为参数调用的。我建议onChange在您的Child组件中注册不同的处理程序来转换相应的输入字段的事件:

控制器组件

class Parent extends React.Component {
    constructor (props) {
        super(props);
        this.state = {} 
    }

    onChange(field, value) {
        // parent class change handler is always called with field name and value
        this.setState({[field]: value});
    }


    render () {
        return <Child onChange={this.onChange.bind(this)} />
    }
}

子组件

class Child extends React.Component {
    constructor (props) {
        super(props);
    }

    onFieldChange(event) {
        // for a regular input field, read field name and value from the event
        const fieldName = event.target.name;
        const fieldValue = event.target.value;
        this.props.onChange(fieldName, fieldValue);
    }

    onDateChange(dateValue) {
        // for a date field, the value is passed into the change handler
        this.props.onChange('dateCommenced', dateValue);
    }

    render () {
        return <form>
          <input type="text" name="jobNumber" onChange={this.onFieldChange.bind(this)} /> 
          <DatePicker onChange={this.onDateChange.bind(this)}  />
        </form>
    }
}
加载输入后如何调用onChange事件?
2021-05-23 18:01:44
正是我一直在寻找的!1+
2021-06-19 18:01:44

import React from 'react'
export default {
  [formData, setFormData] = React.useState({
    name: '',
    number: '',
    email: '',
    address: '',
    zip: ''
  })

  const handleChange = (e) => {
    setFormData({ ...formData, [e.target.name]: e.target.value })
  }
  
  return (
    <div>
      <input type='text' name='name' onChange={handleChange} />
      <input type='text' name='number' onChange={handleChange} />
      <input type='email' name='email' onChange={handleChange} />
      <textarea name='address' onChange={handleChange}></textarea>
      <input type='text' name='zip' onChange={handleChange} />
    </div>
  )
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>