react-native中的对象内的对象存在问题:Undefined不是对象

时间:2019-04-24 20:56:04

标签: reactjs react-native

我有一个读入状态的对象。当我尝试从渲染中的该对象中提取值时,出现错误消息“未定义不是对象”。

所以对象看起来像这样:const currentGrid = {size:{cols:“ 15”,行:“ 15”}}

我使用componentWillMount中的this.setState({currentGrid:currentGrid})将其分配给状态。

在渲染中,我这样做:

render() {
    return (
      <Container>
        <Content>
          <List>
            <ListItem>
              <Body>
                <Text>{this.state.currentGrid.size.cols}</Text>
              </Body>
            </ListItem>
          </List>
        </Content>
</Container>
);
}

当我加载组件时,出现“ TypeError:undefined不是对象(正在评估'this.state.currentGrid.size.cols')

我知道对象已设置为状态。如果我忽略“ cols”,则错误为:

不变式:对象作为React子对象无效(找到:具有键{cols,rows}的对象)。如果要渲染子级集合,请改用Array。

很抱歉,如果这是一个常见错误,但是除非找到将嵌入式对象转换为数组的方法,否则我找不到正确的方法。

1 个答案:

答案 0 :(得分:0)

一种可能性是应用程序第一次运行ERROR 1726 (HY000): Storage engine 'MyISAM' does not support system tables. [mysql.db] 时未定义。在渲染中检查渲染文本之前是否存在

currentGrid