简体   繁体   中英

how to render objects in react native?

I am working on a simple to do application linked to firebase using react native. i have one class with a few methods in it,as far as I can tell by searching online about this problem, it seems to be something related to an output in the render function. but i checked my methods and I am unable to pin down the exact problem. 在此输入图像描述

and this is my class:

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>
);
}
}

The issue is with your renderRow method where you render:

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

Here you are passing an object as a children of Text element instead of a string. That is because you're setting an object under the text key for your data store here:

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

Note that val() here is a reference to an object you've pushed to your firebase instance here (see firebase docs ):

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

So what you perhaps want to do here is to just push this.state.newTodo instead of an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM