0
state = {AnswerQuery : '', answerdetail: []};

    getData() {
        const data = this.props.navigation.getParam('data');
        this.setState({
            data
        });
    }

    renderdata(){
        console.log(this.state.data) // not able to recieve data here
        const {answerdetail} = this.state
        firebase.firestore().collection("queries").doc(this.state.data).collection("answers").onSnapshot((answerSnapShot) => {
            console.log(this.state.data) // recieving data here
            answerSnapShot.forEach((doc) => {
                answerdetail.push(doc.data())
                console.log(doc.data())
                
                
            })
            console.log(this.state.data) // recieving data here
            this.setState(answerdetail)
           // console.log(answerdetail)
            //console.log(anotherentity)
            
        })
    }

    componentDidMount(){
        this.getData()
        this.renderdata()

        }

Receiving value from another screen using getData() function, but not able to use that value inside renderdata() function while passing the data into firebase firestore command, but i am able to access that data inside the firebase firestore command.

this.state.data has no value while passing it in "doc" firebase firestore function doc(this.state.data)

Please help

  • Does this answer your question? [useState set method not reflecting change immediately](https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately) – lawrence-witt May 29 '21 at 13:30
  • No, i am receiving parameter from another screen, and that is assigned to data state using this.setState({data}) inside componentdidMount, I want to use the value of data in firebase firestore command "doc(this.state.data)", but no value received. I can access the value of that data inside the firebase command. As written in code itself using comment. – Abhi Kulshrestha May 29 '21 at 13:39

1 Answers1

1

The renderdata function is called before the state is updated.

You can update your getData to call the renderdata after the state is updated.

getData() {
    const data = this.props.navigation.getParam('data');
    this.setState({
        data
    }, this.renderdata);
}
Aniket Kolekar
  • 383
  • 6
  • 19