简体   繁体   English

ReactJS的惯用方式是什么? 州?

[英]What is the idiomatic ReactJS way to deal with ?initial? state?

I am working, as an exercise, in converting a very, very simple video game from a jQuery (re)implementation to what I would like to be idiomatic ReactJS. 作为练习,我正在将一个非常非常简单的视频游戏从jQuery(重新实现)转换为我想成为惯用的ReactJS的工作。 I am (trying to) use the React tutorial as my reference. 我正在(尝试)使用React教程作为参考。

The jQuery reimplementation stores the state as an array of arrays of characters. jQuery重新实现将状态存储为字符数组的数组。 I am presently trying to get initial state to be rendered in ReactJS. 我目前正在尝试获取要在ReactJS中呈现的初始状态。 Originally I set the getInitialState() to return the data (an array of arrays) originally returned. 最初,我将getInitialState()设置为返回最初返回的数据(数组的数组)。 That didn't work, as ReactJS seems to want an object and not an array ( React.js: arrays and "Critical assumptions about the merge functions have been violated" error ). 那没有用,因为ReactJS似乎想要一个对象而不是一个数组( React.js:数组和“违反了合并功能的关键假设”错误 )。

The tutorial, in the "Tutorial 10" section, uses Array.map(), suggesting that some level of containing Arrays might be helpful: 该教程在“教程10”部分中,使用Array.map(),建议在某种程度上包含Arrays可能会有所帮助:

Now that the data is available in the CommentList, let's render the comments dynamically: 现在,数据在CommentList中可用,让我们动态地呈现注释:

// tutorial10.js
var CommentList = React.createClass({
  render: function() {
    var commentNodes = this.props.data.map(function (comment) {
      return (
        <Comment author={comment.author}>
          {comment.text}
        </Comment>
      );
    });
    return (
      <div className="commentList">
        {commentNodes}
      </div>
    );
  }
});

What is the best way to translate a grid of characters (presently implemented as an array of arrays)? 转换字符网格(当前实现为数组数组)的最佳方法是什么? Do I put the array as a node on this.state (the state is subject to change, so I am setting state rather than props) and then an Array.map() (or something two-dimensional)? 我是否将数组作为this.state的节点(状态可能会发生变化,所以我要设置state而不是props),然后再放置Array.map()(或二维对象)?

What is the idiomatic ReactJS way to deal with ?initial? ReactJS的惯用方式是什么? state? 州?

Ideally, default data is passed as prop(s) to the root component. 理想情况下,默认数据作为prop传递到根组件。 Eg 例如

React.render(
  <MyApp gid={grid} />,
  container
);

However, simply returning it form the getInitialState method is fine as well. 但是,简单地从getInitialState方法返回它也是可以的。

Originally I set the getInitialState() to return the data (an array of arrays) originally returned. 最初,我将getInitialState()设置为返回最初返回的数据(数组的数组)。 That didn't work, as ReactJS seems to want an object and not an array 那没有用,因为ReactJS似乎想要一个对象而不是一个数组

Just make your data one aspect of the state: 只需使数据成为状态的一个方面:

getInitialState() {
  return {
    grid:  this.props.grid
  };
}

If you're using ES2015 synax you can you initialize state directly in constructor function: 如果您使用的是ES2015 synax,则可以直接在构造函数中初始化状态:

constructor() {
  super();
  this.state = {...}
 }

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

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