Why is my this.props.navigation.setParams not working?

I had a similar problem, regarding setting the title of the navigation bar from within the component itself.

The following worked for me.

1) I defined the navigationOptions method (callback method of react navigation to set navigation properties)

When this method runs, you are very likely not to have access to the properties that are needed to set the navigator state, so simply return the current parameters, this is what I did:

static navigationOptions = ({ navigation }) => {
    const { params } = navigation.state;
    return params;
};

2) I implemented the title construction in componentDidMount, setting the state:

componentDidMount(){
    t = "The window title";
    this.props.navigation.setParams({title: t });
}

I think the same could be done in any user interface event, like the onPress, and can be applied to any property.

Important note: What made the difference to me between not working and working was the definition of the navigationOptions method, even if it is a "do-nothing" method (returns what it gets, unchanged)


Now in 2020 it seems like the official documentation of react native suggests that you use route.param to get the params you want.

        const { itemId } = route.params;

This is from the official documentation:

Pass params to a route by putting them in an object as a second parameter to the navigation.navigate function: navigation.navigate('RouteName', { /* params go here */ })

Read the params in your screen component: route.params.

https://reactnavigation.org/docs/params/