【问题标题】:How to write a generic method to handle multiple state changes in React如何编写通用方法来处理 React 中的多个状态更改
【发布时间】:2020-02-10 04:09:17
【问题描述】:

我正在用 React 构建一个运动跟踪器应用程序。

现在,我正在构建 CreateExercise 组件以提交表单,因此我需要更新每个值的状态。为此,我创建了处理这些更改的方法(onChangeUsername、onChangeDescription、onChangeDuration 等...),但我真的不喜欢重复这样的方法。

如何编写更通用的方法来处理此任务?

class CreateExercise extends Component {
  constructor(props) {
    super(props);

    this.state = {
      username: '',
      description: '',
      duration: 0,
      date: new Date(),
      users: []
    }
  }

  onChangeUsername = (e) => {
    this.setState({
      username: e.target.value
    });
  }

  onChangeDescription = (e) => {
    this.setState({
      description: e.target.value
    });
  }

  onChangeDuration = (e) => {
    this.setState({
      duration: e.target.value
    });
  }

  onChangeDate = (date) => {
    this.setState({
      date: date
    });
  }

  onSubmit = (e) => {
    e.preventDefault();

    const exercise = {
      username: this.state.username,
      description: this.state.description,
      duration: this.state.duration,
      date: this.state.date
    }
    console.log(exercise);

    window.location = '/';
  }

  render() {
    return(
      <div>
        <h3>Create New Exercise Log</h3>
        <form onSubmit={ this.onSubmit }>
          <div className='form-group'>
            <label>Username:</label>
            <select 
              ref='userInput'
              required
              className='form-control'
              value={ this.state.username }
              onChange={ this.onChangeUsername }
            >
              { this.state.users.map((user) => (
                <option key={user} value={user}>{user}</option>
                ))
              }
            </select>
          </div>
          <div className='form-group'>
            <label>Description:</label>
            <input 
              type='text'
              required
              className='form-control'
              value={ this.state.description }
              onChange={ this.onChangeDescription}
            />
          </div>
          <div className='form-group'>
            <label>Duration:</label>
            <input 
              type='text'
              className='form-control'
              value={ this.state.duration }
              onChange={ this.onChangeDuration }
            />
          </div>
          <div className='form-group'>
            <label>Date:</label>
            <div>
              <DatePicker 
                selected={ this.state.date }
                onChange={ this.onChangeDate }
              />
            </div>
          </div>
          <div className='form-groupe'>
            <input 
              type='submit'
              value='Create Exercise Log'
              className='btn btn-primary'
            />
          </div>
        </form>
      </div>
    );
  }
}

export default CreateExercise;

【问题讨论】:

    标签: javascript reactjs methods


    【解决方案1】:

    使用partial application,在您的组件中创建一个函数,该函数接受一个字段名称,并返回一个设置状态的函数:

    onChangeValue = field => e => {
      this.setState({
        [field]: e.target.value
      });
    };
    

    用法:

    onChangeUsername = onChangeValue('username');
    
    onChangeDescription = onChangeValue('description');
    
    onChangeDuration = onChangeValue('duration');
    

    您进一步扩展了这个想法以支持onChangeDate

    onChangeValue = (field, valueTransformer = e => e.target.value) => e => {
      this.setState({
        [field]: valueTransformer(e.target.value)
      });
    };
    

    这不会改变其他on 函数,因为默认是获取e.target.value。要使用onChangeDate,我们现在可以更改valueTransformer

    onChangeDate = onChangeValue('date', v => v);
    

    【讨论】:

      【解决方案2】:

      您可以为HTML 元素定义name,并使用它来设置值:

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

      对应的JSX元素:

      <input
        type="text"
        name="description"
        required
        className="form-control"
        value={this.state.description}
        onChange={this.onChange}
      />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-18
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        • 2021-12-13
        • 2018-07-18
        • 2021-04-06
        相关资源
        最近更新 更多