ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript-如何在React Native中呈现对象?

2019-11-19 10:34:36  阅读:243  来源: 互联网

标签:reactjs react-native javascript


我正在使用React Native开发一个链接到Firebase的简单应用程序.我有一个带有一些方法的类,据我可以通过在线搜索了解此问题,这似乎与render函数中的输出有关.但是我检查了方法,无法确定确切的问题. enter image description here

这是我的课:

class ToDo_React extends Component {
  constructor(props) {
    super(props);
  var myFirebaseRef = new Firebase(' ');
  this.itemsRef = myFirebaseRef.child('items');

  this.state = {
    newTodo: '',
    todoSource: new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2})
  };

  this.items = [];
}
componentDidMount() {
  // When a todo is added
  this.itemsRef.on('child_added', (dataSnapshot) => {
    this.items.push({id: dataSnapshot.key(), text: dataSnapshot.val()});
    this.setState({
      todoSource: this.state.todoSource.cloneWithRows(this.items)
    });
  });
  this.itemsRef.on('child_removed', (dataSnapshot) => {
      this.items = this.items.filter((x) => x.id !== dataSnapshot.key());
      this.setState({
        todoSource: this.state.todoSource.cloneWithRows(this.items)
      });
  });
}
    addTodo() {
  if (this.state.newTodo !== '') {
    this.itemsRef.push({
      todo: this.state.newTodo
    });
    this.setState({
      newTodo : ''
    });
  }
}
    removeTodo(rowData) {
  this.itemsRef.child(rowData.id).remove();
}
render() { return (
<View style={styles.appContainer}>
  <View style={styles.titleView}>
    <Text style={styles.titleText}>
      My Todos
    </Text>
  </View>



<View style={styles.inputcontainer}>
    <TextInput style={styles.input} onChangeText={(text) => this.setState({newTodo: text})} value={this.state.newTodo}/>
    <TouchableHighlight
      style={styles.button}
      onPress={() => this.addTodo()}
      underlayColor='#dddddd'>
      <Text style={styles.btnText}>Add!</Text>
    </TouchableHighlight>
  </View>
  <ListView
    dataSource={this.state.todoSource}
    renderRow={this.renderRow.bind(this)} />
</View>
);
}
renderRow(rowData) {
return (
<TouchableHighlight
underlayColor='#dddddd'
onPress={() => this.removeTodo(rowData)}>
<View>
<View style={styles.row}>
  <Text style={styles.todoText}>{rowData.text}</Text>
</View>
<View style={styles.separator} />
</View>
</TouchableHighlight>
);
}
}

解决方法:

问题出在您渲染的renderRow方法上:

<Text style={styles.todoText}>{rowData.text}</Text>

在这里,您将传递一个对象作为Text元素的子代,而不是字符串.这是因为您要在此处的数据存储的文本键下设置一个对象:

this.items.push({id: dataSnapshot.key(), text: dataSnapshot.val()});

请注意,这里的val()是对您在此处已推送到Firebase实例的对象的引用(请参见firebase docs):

this.itemsRef.push({
  todo: this.state.newTodo
});

因此,您可能想在此处执行的操作只是推送this.state.newTodo而不是对象.

标签:reactjs,react-native,javascript
来源: https://codeday.me/bug/20191119/2035611.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有