繁体   English   中英

在react-native keyboardDidHide内部调用函数

[英]Calling a function inside react-native keyboardDidHide

我正在使用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
    );
  }
...
}

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

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

最终,我只想在屏幕键盘关闭时调用this.props.searchCatalog(...)

您丢失了“此”上下文。 您可以使用箭头功能修复该问题:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM