1 、安裝
npm installnpm install --save redux --save redux
2、在src目錄下新建index.js和red ucer.js
index.js中
import { createStore } from 'redux' // 引入createStore方法
import reducer from './reducer'
const store = createStore(reducer) // 創建數據存儲倉庫
export default store //暴露出去
reducer.js中定義數據
const defaultState = {
inputValue : 'Write Something',
list:[
'早上4點起牀,鍛鍊身體',
'中午下班遊泳一小時'
]
}
export default (state = defaultState,action)=>{
return state
}
3、組件中使用
import store from './store/index' //引入
//構造函數中獲取數據
constructor(props){
super(props)
this.state=store.getState();
}
//js中使用
{{this.state.inputValue}}