侧边栏壁纸
  • 累计撰写 197 篇文章
  • 累计收到 496 条评论

如何创建一个store?

2019-11-20 / 0 评论 / 39 阅读

相信很多人不用store很快就忘记了

首先创建store文件夹

在文件夹中创建index.js


import { createStore } from 'redux'
imoprt reducer from './reducer'

const store = createStore(reducer)

export default store;

然后创建reducer.js文件



const defaultState = {
   ...
}

export default (state = defaultState, action) {
    return state;
}


命令:

 在其他的组件如何获取store数据呢?

使用import store from './store'

获取所有数据 store.getState()

订阅:store.subscribe(f())

触发action:store.dispatch({obj})

评论一下?

OωO
取消