【发布时间】:2017-09-10 00:59:38
【问题描述】:
当你有一个表单组件,并且你需要使用导航栏中的按钮提交组件的一部分状态时,你如何处理?
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>
);
}
}
【问题讨论】:
-
你能做 console.log(this.constructor.name) 而不是在完成的新闻上提交吗?确保您有正确的上下文
标签: reactjs react-native react-navigation