在react-native keyboardDidHide中调用一个函数

时间:2018-04-09 22:02:01

标签: javascript reactjs react-native

我正在使用create-react-native-app和Expo构建应用程序,我希望在屏幕键盘关闭时调用搜索功能。

class SearchPage extends Component {
  state = {
    modalVisible: false
  };
  componentDidMount() {
    this.keyboardDidHideListener = Keyboard.addListener(
      'keyboardDidHide',
      this._keyboardDidHide
    );
  }

  componentWillUnmount() {
    this.keyboardDidHideListener.remove();
  }

  _keyboardDidHide() {
    this.onSearchButtonPress();
  }
  onSearchButtonPress() {
    this.props.searchCatalog(
      this.props.search,
      this.props.begins,
      this.props.makes
    );
  }
...
}

我尝试直接在this.props.searchCatalog(...)内部调用_keyboardDidHide(),但每次我都会遇到同样的错误:

“this.onSearchButtonPress不是函数。(在`this.onSearchButtonPress()'中,'this.onSearchButtonPress'未定义)

最终,我想要做的就是在屏幕键盘关闭时调用this.props.searchCatalog(...)

1 个答案:

答案 0 :(得分:0)

你失去了'这个'背景。您可以使用箭头功能修复它:

this.keyboardDidHideListener = Keyboard.addListener(
  'keyboardDidHide',
  () => this.onSearchButtonPress()
);