javascript - 将对象数组定义为 ECMA6/react 上的状态变量

标签 javascript html reactjs ecmascript-6

我尝试制作如下状态对象数组

class DescriptionComponent extends React.Component {


    constructor(props) {
        super(props);
        var initial_object={};
        initial_object['type']={}
        for(var i=0;i<20;i++){
          if(i===0){
            initial_object['type'][i]="Building";
          }
          else if(i===1){
            initial_object['type'][i]="Storage";
          }
          else if(i===2){
            initial_object['type'][i]="Building size";
          }
          else{
            initial_object['type'][i]="Component";
          }
          this.setState(initial_object);



      }

    }

渲染函数

render()
    {
        return (
               //clonable elements
                 <ul className="description-input">
                   {Array(20).fill(1).map((el, i) =>
                   <li >
                     <select name="type" onChange={this.setValue.bind(this, 'type',i)}>
                       <option value="Component">Component</option>
                       <option value="Building">Bulding</option>
                       <option value="Storage">Storage</option>
                       <option value="Building Size">Building size</option>
                     </select>
                     <div className={"int-input" +this.state.type[i]!='Building' ? 'hidden' :''}>
                       <input type="text" name="int_input" onChange={this.setValue.bind(this, 'int_input',i)}/>
                     </div>

                   </li>
                  )}

                 </ul>


        );

我正在尝试预定义状态,以便我可以预加载一些具有默认值的表单元素。

我收到错误Uncaught TypeError: Cannot read property 'type' of null

我想声明类似下面的内容,它也会给出语法错误

this.state={
                  type[0]:'Building',
                  type[1]:'Building',
                  type[2]:'Building',
                  type[3]:'Building',
                  type[4]:'Building',
                  type[5]:'Building',
                  type[6]:'Building',
                  type[7]:'Building',
                  type[8]:'Building',
                  type[9]:'Building',


          }

最佳答案

在构造函数中,您应该直接设置状态,而不是调用setState:

this.state = {};

所以会是:

class DescriptionComponent extends React.Component {
  constructor(props) {
    super(props);
    initial_object['type'] = {};
    for (var i = 0; i < 20; i++) {
      if (i === 0) {
        initial_object['type'][i] = 'Building';
      } else if (i === 1) {
        initial_object['type'][i] = 'Storage';
      } else if (i === 2) {
        initial_object['type'][i] = 'Building size';
      } else {
        initial_object['type'][i] = 'Component';
      }
    }
    this.state = initial_object;
  }
}

错误表明 this.state 未定义

<小时/>

如果你想直接设置它,那么你可以将它写成数组:

this.state = {
  type: [
    'Building',
    'Storage',
    'Building size',
    'Component',
    'Component',
    'Component' // etc..
  ]
};

关于javascript - 将对象数组定义为 ECMA6/react 上的状态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46576006/

相关文章:

html - WrapBootstrap 模板不起作用

javascript - ReactCSSTransitionGroup componentWillLeave 未调用

javascript - $state.go() 运行没有错误,但实际上不适用于 Ionic

javascript - "Maximum call stack size exceeded"方法正确但效率不够? Codewars 卡塔 “Stop the Zombie Apocalypse!”

html - 在css中对齐textarea旁边的输入按钮

node.js - socket.on 没有监听事件

reactjs - 有条件地渲染 TouchableHighlight 的 onPress

javascript - ListBox 项目在 jQuery 中被空数组替换后仍然存在

javascript - 这段代码会产生内存泄漏吗?

javascript - Nightwatch.js 无法将字符串变量设置为输入字段?