mirror icon indicating copy to clipboard operation
mirror copied to clipboard

Global action

Open vanhtuan0409 opened this issue 8 years ago • 3 comments

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

vanhtuan0409 avatar Sep 21 '17 10:09 vanhtuan0409

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)

llh911001 avatar Sep 21 '17 12:09 llh911001

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

vanhtuan0409 avatar Sep 22 '17 02:09 vanhtuan0409

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

llh911001 avatar Sep 22 '17 02:09 llh911001