KBLNY / react-native-message-bar

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

Is it possible to dispatch the ShowAlert function through the reducer? #15

Open duwerq opened 8 years ago

duwerq commented 8 years ago

I'm trying to use the ShowAlert function for logout success messages and error messages. The problem is my actions are tucked away in another file that isn't a child component of my the main parent component. Is there any way to feed the Alert action through the redux store and have RegisterMessageBar function listen for the alert? I didn't know how to classify this as a question as opposed to an issue. I realize in the documentation it specifically mentions it needs to be a child component but I still thought I'd try my luck and ask. Thanks in advance!

gabceb commented 8 years ago

Hi @twolips. I am currently implementing MessageBar via Redux reducers and this is what I'm doing for it:

componentWillReceiveProps (nextProps) {
    const nextMessageBar = nextProps.messageBar
    const { messageBar } = this.props

    if (nextMessageBar && nextMessageBar.message && messageBar !== nextMessageBar) {
      MessageBarManager.showAlert({...nextMessageBar})
    }
}

This code will only fire when some action updates the message bar reducer thus showing what was added to this reducer. Note that the code above is pseudo code so take it with a grain of salt

salhotra commented 7 years ago

@gabceb could you please illustrate this?

gabceb commented 7 years ago

How exactly should I illustrate it @salhotra ? The code I posted should be enough to plug into a project

salhotra commented 7 years ago

@gabceb I was able to get it to work. Thanks