skip to Main Content

Platform: React Native (Expo)

I want to get my params to a new screen using class-based components and React Navigation 6.0.

This is how I am sending the params:

this.props.navigation.navigate("BlogDetails", { blogID: item.ID });

and this is how I’m retrieving it:

componentDidMount() {
    const { blogID } = this.props.route.params;
    this.setState({ blobID: blogID });
    console.log(this.state.blobID);
  }

There are no errors, however, the console.log() returns "".

I am unable to find where I’m going wrong but I’m pretty sure the problem is somewhere during the retrieval, I’m pretty confident that the data is being sent to the screen.

3

Answers


  1. state doesn’t update immediately, if you want use state after update should act like this

    componentDidMount() {
     const { blogID } = this.props.route.params;
     this.setState({ blobID: blogID }, () => {
      console.log(this.state.blobID);
     });
    }
    
    Login or Signup to reply.
  2. const {paramName1, paramName2, ...} = this.props.navigation.state.params
    
    Login or Signup to reply.
  3. It seems different for react navigation version.

    const { blogID } = this.props.route.params;
    

    or

    const blogID = this.props.route.getParam('blogID);
    

    try to use getParam function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search