Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
duwerq opened this issue Jun 3, 2016 · 4 comments
Open

Comments

@duwerq
Copy link

duwerq commented Jun 3, 2016

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
Copy link

gabceb commented Aug 1, 2016

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

  • Assuming you are using immutables on your reducer, create a messageBar reducer that has the properties you want to pass to the message bar as state.
  • On your app container, where you initialize the messageBar, map the new reducer to the state
  • Create a componentWillReceiveProps function and add the following code
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
Copy link

@gabceb could you please illustrate this?

@gabceb
Copy link

gabceb commented Jun 29, 2017

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

@salhotra
Copy link

salhotra commented Jun 29, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants