组件中每个元素的不同handleChange()函数?(阵营

Pet*_*ias 6 javascript reactjs redux

我想知道这是否是一种好的做法,或者我是否应该以不同的方式设计这个应用程序.我特别关注两个'handleChange'函数,我想知道这是否可以以某种方式简化.当然,也欢迎其他建议.

用户add.js:

import React, {Component} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {createUser} from '../actions/index'

class UserCreate extends Component {

    constructor(props, context) {
        super(props, context);
        this.state = {
            inputText: 'Helluuuu'
        }
    }

    handleChangeFirstName(event) {
        console.log(event.target.value);
        this.setState({
            inputTextFirstName: event.target.value
        })
    }

    handleChangeLastName(event) {
        console.log(event.target.value);
        this.setState({
            inputTextLastName: event.target.value
        })
    }

    render() {
        return (
            <div>
                <h2> Add User </h2>

                <table className="userTable">
                <tbody>
                <tr>
                    <td>First Name:</td>
                    <td>Last Name:</td>
                </tr>

                <tr>
                    <td>
                        <input type="text"
                            placeholder="Hello!"
                            value={this.state.inputTextFirstName}
                            onChange={this.handleChangeFirstName.bind(this)}/>
                    </td>
                    <td>
                        <input type="text"
                            placeholder="Hello!"
                            value={this.state.inputTextLastName}
                            onChange={this.handleChangeLastName.bind(this)} />
                    </td>
                </tr>
                </tbody>
                </table>


                <button onClick={() =>this.props.createUser()}>Submit</button>

            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        user: state.activeUser
    };
}

function matchDispatchToProps(dispatch){
    return bindActionCreators({createUser: createUser}, dispatch);
}

export default connect(mapStateToProps, matchDispatchToProps)(UserCreate);
Run Code Online (Sandbox Code Playgroud)

Uma*_*raz 14

当然,您可以将此减少为仅一种handleChange方法,并且可以使用该单个方法消耗所需的任何数量的输入字段.

此外,我认为您不需要任何第三方包.

在你的渲染方法中:

<input 
  type="text"
  name="firstName"
  placeholder="First Name!"
  value={this.state.firstName}
  onChange={this.handleChange.bind(this)}
/>

<input 
  type="text"
  name="lastName"
  placeholder="Last Name!"
  value={this.state.lastName}
  onChange={this.handleChange.bind(this)}
/>
Run Code Online (Sandbox Code Playgroud)

处理变更方法

handleChange(e) {
   this.setState({ [e.target.name] : e.target.value });
}
Run Code Online (Sandbox Code Playgroud)

更清洁.


kra*_*vip 9

请注意复选框的工作方式不同!

来自React 文档

handleInputChange(event) {
    const target = event.target;
    const value = target.type === 'checkbox' ? target.checked : target.value;
    const name = target.name;

    this.setState({
        [name]: value
    });
}
Run Code Online (Sandbox Code Playgroud)

更简洁:

handleInputChange({target}) {
    const value = target.type === 'checkbox' ? target.checked : target.value;
    this.setState({
        [target.name]: value
    });
}
Run Code Online (Sandbox Code Playgroud)


Fab*_*ltz 1

handleChange如果您想在将来提交之前以某种方式修改该数据,两种方法都很好,并且可能会有所帮助。但可以理解的是,为更多表单字段创建所有这些方法可能会很麻烦。幸运的是,有所谓的双向绑定助手。据我了解,它们仍然在 React 的文档中显示 mixins,因此您可能最好使用第三方库,例如react-link-state

import React from 'react';
import linkState from 'react-link-state';

export default MyForm extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      username: '',
      password: '',
      toggle: false
    };
  }

  render() {
    console.log(this.state);

    return (
      <form>
        <input type="text" valueLink={linkState(this, 'username')} />
        <input type="password" valueLink={linkState(this, 'password')} />
        <input type="checkbox" checkedLink={linkState(this, 'toggle')}
      </form>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)