How do you handle cases when you have, say, a form component, and you need to submit a part of the component's state using button in navigation bar?
const navBtn = (iconName, onPress) => (
<TouchableOpacity
onPress={onPress}
style={styles.iconWrapper}
>
<Icon name={iconName} size={cs.iconSize} style={styles.icon} />
</TouchableOpacity>
)
class ComponentName extends Component {
static navigationOptions = {
header: (props) => ({
tintColor: 'white',
style: {
backgroundColor: cs.primaryColor
},
left: navBtn('clear', () => props.goBack()),
right: navBtn('done', () => this.submitForm()), // error: this.submitForm is not a function
}),
title: 'Form',
}
constructor(props) {
super(props);
this.state = {
formText: ''
};
}
submitForm() {
this.props.submitFormAction(this.state.formText)
}
render() {
return (
<View>
...form goes here
</View>
);
}
}
Send a binded function with
setParams
, then you will have access to component'sstate
within that function.Example:
Now you can have a button handler related to component's
state
.Simple Design Pattern
Just as a follow-up to @val's excellent answer, here's how I structured my Component so that all the params are set in the
componentWillMount
. I find this keeps it simpler and is an easy pattern to follow for all other screens.You are getting this error because you are using props and state before declaring
constructor()
. As in constructor we first call super(props) so that we can use props in our component. Please do the following to get desired result.Cheers:)
On your componentDidMount, you can use
Then, pass a function to your header on static props. This function has access to the params you set before
Thanks to rafaelcorreiapoli