简体   繁体   English

将 firebase 读取映射到 props react-native

[英]mapping firebase read to props react-native

this might not be an issue with the mapping itself but i've read some data from my firebase realtime database into my sate and i'm trying to pass it as props then map it in a subsequent component.这可能不是映射本身的问题,但我已经从我的 firebase 实时数据库中读取了一些数据到我的状态中,我试图将它作为道具传递,然后将它映射到后续组件中。

I am getting the following error, I am using an android emulator:我收到以下错误,我使用的是 android 模拟器:

TypeError: undefined is not a function (near ...'this.props.notes.map'...)

app.js (where I update the state) app.js(我更新状态的地方)


state = {
    loggedin: false,
    notes: [
      {
        id: 1,
        text: "mow the lawn",
        author: "dean",
        time: "10am"
      },
      {
        id: 2,
        text: "feed the dog",
        author: "sam",
        time: "2pm"
      }
    ]
  }

//passing props to notes component
<Notes style={styles.notes} notes={this.state.notes} />


updateNotes = async () => {

    console.log("grabbing new notes");
    const snapshot = await firebase.database().ref('Users/Notes').once('value');
    console.log(snapshot.val())
    this.setState({ notes: snapshot.val() });
  };

my Notes component where I map the props我的 Notes 组件,我在其中映射了道具

renderCondition =()=>{
    if(this.state.Deleted === false){
      return(
        <View>
        {this.props.notes.map(note => (
          <View
            style={styles.note}
            key={note.author}
            id={note.id}
          >
            <Text style={styles.noteHeader}>{note.author}</Text>
            <Text style={styles.noteText}>{note.text}</Text>

              <Text style={styles.noteTime}>{note.time}</Text>
              <Button title= 'X' onPress={() => this.setState({Deleted:true}) }></Button>
          </View>

        ))}
      </View>
      )
        }

      return(
      <View>
        <Text>are you sure you want to delete this note?</Text>
        <Button title="Yes"></Button>
        <Button onPress ={()=>{this.setState({Deleted:false})}} title="No"></Button>
      </View>
      )
  }

render() {
    return (
      <View>
      {this.renderCondition()}
      </View>
    );
  }

You have to check whether the notes have been passed down or whether they are undefined or null .您必须检查notes是否已传递或它们是否为undefinednull JavaScript is not going to map an undefined object. JavaScript 不会映射undefined对象。

Try the following code:试试下面的代码:

{this.props.notes && this.props.notes.map(note => (
    // do stuff with each note...
))}

This will run the .map function only if the notes are neither undefined nor null .仅当notes既不是undefined也不是null才会运行.map函数。

So my issue was that I needed to update my notes state by making sure it was an array.所以我的问题是我需要通过确保它是一个数组来更新我的笔记状态。 I had mistakenly simply updated it as an object and then attempted to map the object.我错误地简单地将它更新为一个对象,然后尝试映射该对象。 Here is my solution.这是我的解决方案。

from this由此

updateNotes = async () => {

    console.log("grabbing new notes");
    const snapshot = await firebase.database().ref('Users/Notes').once('value');
    console.log(snapshot.val())
    this.setState({ notes: snapshot.val() });
  };

to this对此

updateNotes = async () => {

    console.log("grabbing new notes");
    const snapshot = await firebase.database().ref('Users/Notes').once('value');
    console.log(snapshot.val())
    this.setState({ notes: [snapshot.val()] });
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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