例如,当您有一个表单组件并且需要使用导航栏中的按钮提交组件的一部分状态时,您如何处理这种情况?
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>
);
}
}