简体   繁体   English

React.js 状态混乱

[英]React.js state confusion

I am still trying to understand the state concept in react.js.我仍在尝试理解 react.js 中的状态概念。 Can anyone please help with the below jsfiddle?任何人都可以帮助以下jsfiddle吗? I am trying to filter the records based on the category selected.我正在尝试根据所选类别过滤记录。

var App = React.createClass({
    render: function() {
        return (
        <div>
            <Instructions />
            <h1>Requests</h1>
        </div>
        );
    }
});

From what I've found with react, communicating changes between components that don't have a parent-child relationship kind of requires the state to be managed in a top-level component that is a parent to both the components that are trying to communicate.根据我在 React 中的发现,在没有父子关系的组件之间进行更改通信需要在顶级组件中管理状态,该组件是尝试通信的组件的父级. In your example, App is your top-level component that contains MySelect and DisplayRecords as children.在您的示例中, App是包含MySelectDisplayRecords作为子项的顶级组件。 If you want the status of MySelect to affect the rows shown in DisplayRecords , you'll have to manage that in the state of App .如果您希望MySelect的状态影响DisplayRecords显示的行,则必须在App状态中进行管理。

In the example, I moved the select box' selection to the state of App , and passed props to the different components accordingly.在示例中,我将选择框的选择移动到App的状态,并相应地将 props 传递给不同的组件。 I tried my best to explain the notable changes with comments, but if you have questions about any changes, definitely leave a comment!我尽力用评论来解释显着的变化,但如果你对任何变化有疑问,一定要发表评论!

var DisplayRecords = React.createClass({
  render: function(){
    var _this = this; // avoid conflicting this keyword
    return (
        <div>
        <table><tbody> // include tbody to avoid errors (weird react thing)
         {_this.props.records.map(function(record){ // loop through each record
            // if all records is selected, or the record status matches the selection
            if(_this.props.filter=="All Requests" || record.status == _this.props.filter){
              // return the record as a table row
              return (
                <tr key={record.id} >
                <td>{record.title}</td>
                <td><a href="#">{record.status}</a></td>
                <td>{record.updated_at}</td>
                <td>{record.created_at}</td>
                <td><a href="#">Delete</a></td>
                </tr>
              )
            }
          })}
        </tbody></table>
     </div>
    )
  }
});

var MySelect = React.createClass({
  callParentFunction: function(e) {
    // call parent's getFilter function with the selected option's text as argument
    this.props.changeHandler(e.target.options[e.target.selectedIndex].text);
  },
  render: function() {
    // note removed specified value of select box
    return (
      React.createElement("select", { onChange: this.callParentFunction},
         React.createElement("option", { value: 1 }, "All Requests"),
         React.createElement("option", { value: 2 }, "Approved"),
         React.createElement("option", { value: 3 }, "Denied"),
      React.createElement("option", { value: 4 }, "Pending")
      )
    )
  }
});


var App = React.createClass({

  getInitialState: function(){
    // set initial selection
    return {
     selected: "All Requests"
    }
  },
  getFilter:function(newFilter){
    // set new selection on change of select box
    this.setState({selected: newFilter})
  },
  render: function() {
    // pass selected state to both MySelect and DisplayRecords
    // pass getFilter to MySelect so it can be called onChange
    return (
      <div>
        <MySelect selection={this.state.selected} changeHandler={this.getFilter} />
        <h1>Requests</h1>
          <DisplayRecords records={this.props.data} filter={this.state.selected} />
      </div>
    );
  }
});
React.render(<App data={requests}/>, document.getElementById('container'));

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

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