Access State inside of mapDispatchToProps method

前端 未结 5 1839
清歌不尽
清歌不尽 2021-01-30 16:37

I have written a container component using redux and my implementation for mapDispatchToProps looks like this

const mapDispatchToProps = (dispatch, o         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-30 17:04

    You can just use redux-thunk to get state. Write a helper function like this:

    const getState = (dispatch) => new Promise((resolve) => {
      dispatch((dispatch, getState) => {resolve(getState())})
    })
    

    You can use this in a async function or generator function:

    const mapDispatchToProps = (dispatch, ownProps) => {
      return {
        async someFunction() {
          const state = await getState(dispatch)
          ...
        }
      }
    }
    

提交回复
热议问题