在react-native中,我无法从单独的类中调用ref

时间:2019-03-24 19:21:30

标签: react-native

在我的主要app.js中,我有我的吐司组件,因此它在所有其他层之上 app.js

import toast from 'react-native-easy-toast'

export default class App extends React.Component {
constructor(props){
    super(props)
    this.showToast = this.showToast.bind(this)
  }



  render() {
      return (
        <View style={styles.container}>
          {Platform.OS === 'ios' && <StatusBar barStyle="default" />}
          <AppNavigator />
          <Toast ref="toast" position='bottom' />
        </View>
      );
  }

  showToast = (data, duration) => {
    this.refs.toast.show(data, duration)
  }
}

我正试图用

来称呼它

register.js

import App from '../../App'

export default class register extends react.component {

     showError = (error) => {
           new App().showToast(error, 1000);
     }

}

我收到以下错误:“未定义不是对象(正在评估'_this.refs.toast.show')”

在app.js的行上:

this.refs.toast.show(data, duration)

0 个答案:

没有答案