Talor-A / react-native-message-bar

A notification bar alert displayed at the top of the screen for react-native
MIT License
55 stars 27 forks source link

undefined: this_currentMessageBarAlert.hideMessageBarAlert #24

Closed ikendoit closed 6 years ago

ikendoit commented 6 years ago

Hello, I was trying out the message bar and i get this error :

screenshot_1514530068

my testMess():

66.  testMess(){
  67.   MessageBarManager.showAlert({
        title: "title jere", 
        message: "a message for a message", 
        alertType: "success",
    });
    console.log("did a message show ??? ");
  }

My ComponentDidMount:

  componentDidMount() {
    this.setState({
      ..........
    });
    MessageBarManager.registerMessageBar(this.refs.alert);
    this.testMess();
  }

I set up everything as told in the manual but this didn't work. Has anyone had this problem before ? i tried playing around with the MessageBarManager.js but nothing worked.

hamidfzm commented 6 years ago

I have the same problem. I think alert reference register has problem.

Talor-A commented 6 years ago

are you sure that this.refs.alert is != undefined? that's the only way I can see this error happening.

hamidfzm commented 6 years ago

I think it is a binding problem. I used https://github.com/Qlean/react-native-messages. It's a simple message bar (So simple compared to this project) but they implemented a better code( or binding solution) for message bar manager. Please take a look at their source code.

ugendrang commented 6 years ago

+1

Talor-A commented 6 years ago

@hamidfzm thanks for showing me that library, it's definitely a better solution. I unfortunately don't have the time to do any major refactoring of this library right now, but I'll look into this issue again. unfortunately so far I haven't been able to reproduce the problem, but if anyone wants to share a simple demo project I'll take a look.

blntylmn commented 6 years ago

Anyone has issue with this problem. You can surround the registerMessageBar function in a setTimeout function like below :

componentDidMount() {
    setTimeout(() => {
      MessageBarManager.registerMessageBar(this.alert);
    }, 0);
  }

and don't use ref as string

<MessageBarAlert  ref={(a) => { this.alert = a; }} />

@ikendoit don't use setState in your componentDidMount. If you really need it in componentDidMount; surround it with setTimeout 👍

ikendoit commented 6 years ago

Thank you @blntylmn , the way you used setTimeout was very clever ! I will surely try it your way.

xxzefgh commented 6 years ago

You can just register in ref callback:

<MessageBar
  ref={ref => {
    MessageBarManager.registerMessageBar(ref);
  }}
/>
phillbaker commented 5 years ago

This seems similar to the upstream issue: https://github.com/KBLNY/react-native-message-bar/issues/46