How to call a function 'this' when my function does not recognize?

Asked

Viewed 276 times

0

In the question was difficult to explain, but my problem is the sequinte, I added in my component the keyboard component. However in the function _keyboardDidHide I am calling another function that is in props. But it generates an error saying that this.props.closeModal() is undefined, among other words it is as if it is not stated, but I am sure that this function works, because my modal in onRequestClose calls her normally and works.

It’s like anything I put inside the function _keyboardDidHide did not share the same variables of the scope of my Component. Is there any solution to this?

  componentDidMount() {
    this.keyboardDidShowListener = Keyboard.addListener(
      'keyboardDidShow',
      this._keyboardDidShow,
    );
    this.keyboardDidHideListener = Keyboard.addListener(
      'keyboardDidHide',
      this._keyboardDidHide,
    );
  }

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

  _keyboardDidShow() {
    alert('Keyboard Shown');
  }

  _keyboardDidHide() {
    this.props.closeModal();
  }

  render() {
    return (
      <Modal
          transparent={true}
          visible={this.props.modalVisible}
          style={styles.container}
          onRequestClose={() => {
            this.props.closeModal();
          }}>
        <TextInput
          style={styles.input}/>
      </Modal>
    );
  }
}

  • 2

    Vinicius, have you tried to bind _keyboardDidHide with this from your class? Type this. _keyboardDidHide.bind(this). When using Act I had to do this several times...

  • 1

    The this represents element or event that is occurring within the current scope, you can use the.log() console to see what this is currently coming from.

  • I got it, I had to do it in the library: () => this._keyboardDidHide().

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.