简体   繁体   English

在React Js中检索一系列不同子组件数据的最佳方法

[英]The best way to retrieve data of a range of different child components in React Js

I am quite new to react js. 我是反应js的新手。 Have searched a bit but haven't got an answer to this question: 搜索了一下,但没有得到以下问题的答案:

Suppose I have two dynamic input tables in my page, and each of them is a separate component. 假设我的页面中有两个动态输入表,每个表都是一个单独的组件。 And the lines are different components (React classes) as well. 这些行也是不同的组件(React类)。 And on the page, there is a save button. 并且在页面上,有一个保存按钮。 Once the save button is clicked, all the information of the page should be gathered and pushed to server as a JSON string. 单击保存按钮后,应收集页面的所有信息并将其作为JSON字符串推送到服务器。

The very obvious approach for me is to gather the information via jQuery. 对我来说,非常明显的方法是通过jQuery收集信息。 This will definitely work. 这肯定会工作。 But it makes me feel it is not the react way of doing it. 但这让我感到这不是这样做的反应方式。 Since react data is one way binding, I am not quite sure how to handle this situation more appropriately. 由于反应数据是绑定的一种方式,因此我不确定如何更适当地处理这种情况。 Any suggestions? 有什么建议么?

As always, there are a few ways of doing this. 与往常一样,有几种方法可以做到这一点。

1. Using refs . 1.使用refs

You can assign references to input fields and then loop through them to get the values. 您可以将引用分配给输入字段,然后遍历它们以获取值。 The good thing about this approach is that no events are needed, but you still have to somehow know the reference to each. 这种方法的优点是不需要任何事件,但是您仍然必须以某种方式知道对每个事件的引用。 It adds complexity if you have dynamic fields or heavily nested fields. 如果您有动态字段或大量嵌套的字段,则会增加复杂性。 This is still my preferred way, mainly because you don't need events (eg keyup, blur, change, depending on your usage) 这仍然是我的首选方式,主要是因为您不需要事件(例如,抠像,模糊,更改,具体取决于您的用法)

2. Using state 2.使用状态

This makes it easier to instantly get values, if the values are updated in state as soon as the user makes a change to the field. 如果在用户更改字段后立即更新状态,则可以更轻松地立即获取值。 Obviously you will need to know when a change has been made so you need events. 显然,您将需要知道何时进行了更改,因此需要事件。

Your event callback can do one of many things, such as 您的事件回调可以执行许多操作之一,例如

  • update a global state object (eg via redux) 更新全局状态对象(例如,通过redux)
  • update form's values (or state) object via context usage 通过上下文用法更新表单的值(或状态)对象

I hope this helps plan your forms. 我希望这有助于您计划表格。

Please check out this simplified example: https://jsfiddle.net/352v4n72/2/ 请查看以下简化示例: https : //jsfiddle.net/352v4n72/2/

It has three components. 它包含三个部分。 The most basic one is Input , which informs its parent when the value is changed: 最基本的是Input ,它在值更改时通知其父级:

var Input = React.createClass({
  render: function() {
    return (
        <input type="text" placeholder={this.props.name}
            onChange={e => this.props.onInputChange(e.currentTarget.value)} />
    );
  }
});

Now, its parent component, Line : 现在,其父组件Line

var Line = React.createClass({
  onInputChange(inputId, value) {
    var obj = {};
    obj[inputId] = value;
    this.setState(obj, state => {
      this.props.onLineChange(this.props.lineId, this.state);
    });
  },
  render: function() {
    return (
        <div>
        <Input name="firstName" onInputChange={value => this.onInputChange('firstName', value)} />
        <Input name="lastName" onInputChange={value => this.onInputChange('lastName', value)} />
        <Input name="email" onInputChange={value => this.onInputChange('email', value)} />
      </div>
    );
  }
});

This component holds three Input components. 该组件包含三个Input组件。 When each of them changes, the onInputChange function is called, and this function basically aggregates all the input values, creating a whole "line" data. 当它们每个都更改时,将onInputChange函数,该函数基本上会汇总所有输入值,从而创建整个“行”数据。

The last component is Table : 最后一个组件是Table

var Table = React.createClass({
  onLineChange(lineId, value) {
    var obj = {};
    obj[lineId] = value;
    this.setState(obj, state => console.log(this.state));

  },
  render: function() {
    return (
        <div>
        <Line lineId={1} onLineChange={this.onLineChange} />
        <Line lineId={2} onLineChange={this.onLineChange} />
        <Line lineId={3} onLineChange={this.onLineChange} />
      </div>
    );
  }
});

This component holds three lines, and just like the line aggregates Input 's, Table aggregates Line 's. 该组件包含三行,就像该行聚合Input的行, Table聚合Line You can see the state as it changes in the console. 您可以在控制台中查看状态的变化。

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

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