Shibin Raju Mathew
Shibin Raju Mathew

Reputation: 930

How to pass state to static navigation in react native?

Need to pass state to navigation but this.state. not working, here is the code

      this.setState({
      demo:"some title"
    });
.....

 static navigationOptions = {
        headerRight:(<View>
          <Text>{this.state.demo}</Text>
                  </View>),
        };

Upvotes: 2

Views: 863

Answers (2)

Amjad Rehman A
Amjad Rehman A

Reputation: 848

try to set param something like this :

componentWillMount() {
     const {setParams} = this.props.navigation;
     setParams({demotxt:this.state.demo});
}

..

static navigationOptions = ({ navigation  }) => {

       const {state} = navigation;
           return {
               headerRight: (<View>
                 <Text>{state.params.demotxt}</Text>
                         </View>)
           }

};

Upvotes: 2

codemt
codemt

Reputation: 481

I have modified this line, you should call it like this.

<Text>{this.state.demo}</Text>
                  </View>),

Also, you should first declare your state in constant, and then pass the Const, whenever setState is called. the render function will be invoked. Something like this.

 static navigationOptions = {
const title = this.state.demo;
            headerRight:(<View>
              <Text>{this.state.demo}</Text>
                      </View>),
            };

Upvotes: 0

Related Questions