简体   繁体   English

如何处理 React Forms 组件

[英]How to handle React Forms components

I'm trying to make a simple form but after 10 examples/tutorials I still not understand some aspects:)我正在尝试制作一个简单的表格,但是在 10 个示例/教程之后,我仍然不了解某些方面:)

Scenario: I'm getting data with redux action/reducer so the model is coming in props.reduxResponse in this form:场景:我正在使用 redux 动作/减速器获取数据,因此 model 以这种形式出现在 props.reduxResponse 中:

reduxResponse: { 
    name: "123", 
    anotherField: "456"
}

the main component where I get data ( and I hope I can make the update call:) ):我获取数据的主要组件(我希望我可以进行更新调用:)):

class Service extends Component {

  state = {
    reduxResponse: {}
  }

  componentDidMount() {
    this.props.getService(this.props.match.params.id);
  }

  handleSubmit = service => { this.props.saveService(service) }

  render() {
    let { reduxResponse = {} } = this.props;

    return (
      <div>
        <h3>Infos</h3>
        <ServiceBasicInfoForm 
          service = { reduxResponse }
          handleSubmit={(service) => this.handleSubmit(service)}/>
      </div>
    );
  }
}

const mapStateToProps = state => ({
  getService: PropTypes.func.isRequired,
  saveService: PropTypes.func.isRequired,
  reduxResponse: state.reduxResponse.payload
});
export default withRouter(
  connect(mapStateToProps, { getService, saveService })(Service)
);

and the form和表格

class ServiceBasicInfoForm extends Component {
  state = {
    service: {
      name: "",
      anotherField: {},
    }
  };

  handleSubmit = event => {
    props.handleSubmit(service); 
  };

  componentDidUpdate(prevProps, prevState, snapshot) {
    if(this.props.service != prevProps.service){
      this.setState({ service: this.props.service });
    }
  }

  handleChange = event => {
    event.persist();
    this.setState({ [event.target.name]: event.target.value });
  };

  render() {

    let { service = {} } = this.props;

    return (
      <div>
        <ValidatorForm
          ref="form"
          onSubmit={this.handleSubmit}
          onError={errors => null}
        >
          <Grid container spacing={6}>
            <Grid item md={4} spacing={6}>
              <TextValidator
                value={this.state.service.name}
                ..... other props
              />
            </Grid>

            ..... other fields

          </Grid>
        </ValidatorForm>
      </div>
    );
  }
}

export default ServiceBasicInfoForm;

and I can't understand exactly how can I pass the props.reduxResponse from Service to ServiceBasicInfoForm, then edit those values, handle submit and how the returned values after submit will be updated in ServiceBasicInfoForm我不明白如何将 props.reduxResponse 从 Service 传递到 ServiceBasicInfoForm,然后编辑这些值,处理提交以及提交后返回的值将如何在 ServiceBasicInfoForm 中更新

This is a sample example because the Service will contain ~4 forms.这是一个示例,因为服务将包含 ~4 forms。

I think there are a number of issues with your code.我认为您的代码存在许多问题。 I'll try to address all of them.我会尝试解决所有这些问题。 First of all, mapStateToProps maps the Redux state to the component props.首先, mapStateToProps将 Redux state 映射到组件 props。 You have put the mapped object reduxResponse in your component state.您已将映射的 object reduxResponse放入组件 state 中。 In your render, you're also overwriting the mapped prop by assigning in the value {} .在您的渲染中,您还通过分配值{}来覆盖映射的道具。 When creating ServiceBasicInfoForm , your passing service as a prop, but as far as I can see this has not been defined anywhere.创建ServiceBasicInfoForm时,您将传递service作为道具,但据我所知,这尚未在任何地方定义。 What is this supposed to be?这应该是什么? If you want to pass reduxResponse , then just do that:如果你想通过reduxResponse ,那么就这样做:

<ServiceBasicInfoForm reduxResponse= {reduxResponse}/>

This would then be accessible in ServiceBasicInfoForm with:然后可以在 ServiceBasicInfoForm 中访问:

const {reduxReponse} = this.props;

As for 'editing' props - you should never do this.至于“编辑”道具 - 你永远不应该这样做。 The internal state of a component should be kept its state, not its props.组件的内部 state 应保留其 state,而不是其 props。 If you want ServiceBasicInfoForm to communicate back to Service at a certain point, let Service pass in a callback function has a prop to ServiceBasicInfoForm:如果您希望 ServiceBasicInfoForm 在某个时刻与 Service 进行通信,让 Service 传入一个回调 function 有一个 ServiceBasicInfoForm 的属性:

<ServiceBasicInfoForm reduxResponse={reduxResponse} callback={data => this.handleCallbackData(data)}/>

Now, if you need ServiceBasicInfoForm to be rendered differently based on whether a submit has been made, you could just put this in the local state of ServiceBasicInfoForm, and render the component based on its local state.现在,如果您需要根据是否已提交来以不同方式呈现 ServiceBasicInfoForm,您可以将其放在 ServiceBasicInfoForm 的本地 state 中,并根据其本地 state 渲染组件。

Since you're using redux, you may also consider skipping the passing of these props alltogether.由于您使用的是 redux,因此您也可以考虑一起跳过这些道具的传递。 Instead you can connect ServiceBasicInfoForm to the redux state and update this when data arrives (using mapDispatchToProps), and let the the outer Service component listen to the global state (using mapStateToProps).相反,您可以将 ServiceBasicInfoForm 连接到 redux state 并在数据到达时更新此内容(使用 mapDispatchToProps),并让外部服务组件侦听全局 Z9ED39E2EA931582EF.573E(使用 mapStateToPro4)573E

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM