Swift ReactNative:在右侧按钮的导航栏中,单击事件不工作,这是一个错误

Swift ReactNative:在右侧按钮的导航栏中,单击事件不工作,这是一个错误,swift,react-native,Swift,React Native,反应型: 我使用反应导航组件 在右侧按钮的导航栏中,单击事件不起作用 `_newCustomer() { alert('点击新建'); } static navigationOptions = { title: '客户列表', headerRight: ( <Button title={'添加客户'} onPress={this._newCustomer.bind(this)}/>

反应型: 我使用反应导航组件

在右侧按钮的导航栏中,单击事件不起作用

`_newCustomer() {
        alert('点击新建');
      }
    static navigationOptions = {
        title: '客户列表',
        headerRight: (
        <Button title={'添加客户'} onPress={this._newCustomer.bind(this)}/>
          ),
      }

问题是您试图从静态函数调用
this.\u newCustomer
,而
this
未定义,因为您处于静态函数中


如果你看一下,你会发现在例子中他们使用匿名函数。这样做,或者调用另一个静态函数。

静态导航选项无法链接动态“this”变量。通常,您需要创建一个自定义按钮组件,并使用该组件来处理单击事件

static navigationOptions = ({ navigation }) => {
  return {
    headerRight: (
      <EventButton
        navigation={navigation}
      />
    ),
  };
};
静态导航选项=({navigation})=>{
返回{
头灯:(
),
};
};
然后

export const EventButton = (props) => {
let testButton = <TouchableHighlight onPress={() => props.navigation.navigate('CreateNewCustomer',{ name: 'CreateNewCustomer'})}>
  </TouchableHighlight>
  return testButton 
}
export const EventButton=(道具)=>{
让testButton=props.navigation.navigate('CreateNewCustomer',{name:'CreateNewCustomer'})}>
返回测试按钮
}
export const EventButton = (props) => {
let testButton = <TouchableHighlight onPress={() => props.navigation.navigate('CreateNewCustomer',{ name: 'CreateNewCustomer'})}>
  </TouchableHighlight>
  return testButton 
}