javascript-如何在React Native中呈现对象?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-如何在React Native中呈现对象?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2405字,纯文字阅读大概需要4分钟。
内容图文
我正在使用React Native开发一个链接到Firebase的简单应用程序.我有一个带有一些方法的类,据我可以通过在线搜索了解此问题,这似乎与render函数中的输出有关.但是我检查了方法,无法确定确切的问题.
这是我的课:
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而不是对象.
内容总结
以上是互联网集市为您收集整理的javascript-如何在React Native中呈现对象?全部内容,希望文章能够帮你解决javascript-如何在React Native中呈现对象?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。