【发布时间】:2020-01-10 15:16:55
【问题描述】:
我目前正在尝试将 React 组件添加到我的多维数组的第三层。我的结构如下:
constructor(props) {
super(props);
/**
* The state variables pertaining to the component.
* @type {Object}
*/
this.state = {
structure : [ // Block
[ // Slot 1
[ // Layer 1
<Text>text 1</Text>, // Text1
]
]
],
};
}
this.state.structure 包含slots 的数组,其中包含layers 的数组,其中包含由Text 组件组成的数组。
我尝试同时使用concat() 和push() 来设置structure 数组,但都给我错误,例如“无法读取未定义的属性”等。这是我当前的addText()功能:
addText() {
this.setState((previousState) => ({
structure : previousState.structure[0][0].concat(<Text>text2</Text>),
}));
}
在我的应用程序中按下按钮时会调用addText() 函数。
我还直接通过数组在当前层渲染我的Text 组件数组:
{ this.state.structure[0][0] }
我觉得我直面问题,但不知道是什么导致了问题。我希望能够将另一个 Text 添加到容器中,但我尝试做的任何事情似乎都不起作用。
【问题讨论】:
标签: javascript arrays typescript react-native multidimensional-array