redux的使用流程

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}}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章