【问题标题】:Expo TypeError this.state.txt.map in not an objectExpo TypeError this.state.txt.map in not an object
【发布时间】:2020-01-29 06:36:26
【问题描述】:

我正在尝试在我的项目中显示一个数组,但遇到了错误。

这是我关于这个问题的所有代码。

  state = {
    isModalVisible: false,
    txt: ["Hello", "My", "Friend"],
    tuxt: ["Whats","up","Bussy"],
    key: [0,1,2]

  };

renderFeed = () =>{
    return this.state.txt.map((card) => {
      return (
          <Text>{card}</Text>
        )
    })
  }

<View style={{ flexDirection: 'column', marginTop: 5}}>
{this.renderFeed()}
</View>

我不完全确定为什么会遇到这个问题。我到处看到用户遇到这个问题的地方是因为他们忘记将map 函数分配给数组,但是我已经这样做了。

奖励如果有人可以教我如何使用此方法创建一个包含多个对象的&lt;Card&gt; 对象。我真的很感激。我不确定如何使用这种方法引用多个数组(我对 expo 和 react-native 很陌生)。

【问题讨论】:

标签: arrays react-native expo


【解决方案1】:

我找到了解决方案。我将state 对象输入到constructor(props) 方法中:

      constructor(props) {
        super(props);
        this.state = {
            txt: ["Hello", "My", "Friend"],
            tuxt: ["Whats","up","Bussy"],
            key: [0,1,2]
        }
    }

这解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    相关资源
    最近更新 更多