避免多个引用组件中的道具

时间:2017-08-18 07:17:59

标签: reactjs redux

一个典型的组件是这样的,我复制我的道具,是否有一个最好的做法,只为组件获得一次道具?而不是在submitForm()

中再次设置它们
class Screen3 extends Component {
  constructor() {
    super();
    this.submitForm = this.submitForm.bind(this);
    this.onChange = this.onChange.bind(this);
  }

  submitForm(e) {

    const age = this.props.age;
    const gender = this.props.gender;
    const ethnicity = this.props.ethnicity;

    ....
  }

  onChange(e) {
    const field = e.target.name;
    const value = e.target.value;

    ...
  }

  render() {
    let { age, gender, ethnicity } = this.props;

   ....


const mapStateToProps = store => {
  return {
    age: store.UserDetails.age,
    gender: store.UserDetails.gender,
    ethnicity: store.UserDetails.ethnicity

  };
};

0 个答案:

没有答案