首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么object prop不显示在平面列表中?

为什么object prop不显示在平面列表中?
EN

Stack Overflow用户
提问于 2019-10-22 21:47:50
回答 1查看 30关注 0票数 0
代码语言:javascript
运行
复制
    this.state={

      task : "",
      the_array : []


    };
  }

  handleTextChange=(some_task) =>{

    this.setState({
      task : some_task

    });
  }

  pushToList = () => {
    let taskitem = {
      name: this.state.task,
    };
    this.setState({
      the_array: [...this.state.the_array, taskitem],
    });
  }

  render() {
    return(
      <View style={{backgroundColor:'powderblue', height:'100%'}}>
        <FlatList data = {this.state.the_array}
          renderItem={(item) => <Text>{item.name}</Text>} keyExtractor={(item) => item.name} >
        </FlatList>        
        <TextInput style={{ backgroundColor: 'lightsteelblue', height:60, borderRadius : 25, marginBottom:20}} 
        onChangeText={this.handleTextChange}>
        </TextInput>

        <TouchableOpacity style={{
          backgroundColor: 'mediumpurple', height: 60, width: 80, alignSelf: 'center', borderRadius: 20, justifyContent: 'center',
          alignItems: 'center', marginBottom:20
        }} onPress={this.pushToList}>

这是我的代码。我正在尝试将文本输入内容添加到Flatlist中。为此,我在我的按钮onPress方法(‘pushToList’)中定义了一个名为'taskitem‘的对象,并为它设置了一个名为'name’的属性。“pushTolistMethod”应该把“名字”放到屏幕上的平面列表中。但奇怪的是,它不工作,当我按下按钮时没有任何反应。我想知道有没有人能帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-22 21:56:02

你能像这样替换你的扁平列表代码并尝试吗?

代码语言:javascript
运行
复制
    <FlatList data = {this.state.the_array}
      renderItem={({ item }) => <Text>{item.name}</Text>} keyExtractor={(item) => item.name} >
    </FlatList>  

数据在项目键上,所以我们使用解构从函数中访问它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58505796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档