在ECMA6 / react上将对象数组定义为状态变量(Define array of object as state variables on ECMA6/react)

编程入门 行业动态 更新时间:2024-10-22 13:38:12
在ECMA6 / react上将对象数组定义为状态变量(Define array of object as state variables on ECMA6/react)

我试图使状态对象数组如下所示

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', }

I tried to make state object array as below

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); } }

The render function

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> );

I am trying to predefine the states so that I can pre load some form element with default values .

I got error Uncaught TypeError: Cannot read property 'type' of null

I want to declare something like below which is giving syntax error as well

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.. ] };

In the constructor, you should set the state directly instead of calling setState:

this.state = {};

So it would be:

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; } }

The error is saying that this.state is undefined


If you want to set it directly, then you would write it as an array:

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

更多推荐

本文发布于:2023-07-28 19:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   上将   变量   定义   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!