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

Global action #59

Open
vanhtuan0409 opened this issue Sep 21, 2017 · 3 comments
Open

Global action #59

vanhtuan0409 opened this issue Sep 21, 2017 · 3 comments

Comments

@vanhtuan0409
Copy link

vanhtuan0409 commented Sep 21, 2017

Is it possible to have an event that can be listened by multiple models 😄
For example, I have a listener that fire action every time screen media query changed. This action needs to be listened by multiple ui state models

@llh911001
Copy link
Member

llh911001 commented Sep 21, 2017

Yes of course, just connect mediaQuery model's state for your ui state.

For your example, here is a possible solution:

import mirror, { actions } from 'mirrorx'

mirror.model({
  name: 'mediaQuery',
  initialState: {
    width: 0,
    height: 0
  },
  reducers: {
    change(state, data) {
      return { ...state, ...data }
    }
  }
})

// in your media query change event
actions.mediaQuery.change({ width, height })

// components that need to update when media query change
import Comp from './components/Comp'

const MyComponent = connect(state => {
  return {
    mediaQuery: state.mediaQuery,
    stateOfComp: state.stateOfComp
  } 
})(Comp)

@vanhtuan0409
Copy link
Author

vanhtuan0409 commented Sep 22, 2017

Your suggestion is what I doing right now
But can 1 model reducers listen to action of another model
Like can stateOfComp model listen to action from mediaQuery model

@llh911001
Copy link
Member

You can. Try the mirror.hook API, though you should be careful to update state in hooks.

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

2 participants