建立state.js:
let state =
export default state;
定義action型別名常量count.js:
export const add_action ='add_action'
export const remove_action = 'remove_action'
export const change_action ='change_action'
定義action物件actioncreator.js:
import from "./const"
let createaction =
}, removeaction(id)
}, changeaction(id) }}
export default createaction;
定義執行對應的action的reducer.js:
import from "./const"
import _state from "./state"
let reducer = (state=_state,action)=>,state);
switch(action.type))
break;
default:break;
} localstorage.todos = json.stringify(new_state.todos);
localstorage.count = new_state.count;
return new_state;
}export default reducer;
安裝redux,建立乙個倉庫例項,傳入reducer,store.js:
import from 'redux'
import reducer from "./reducer";
let store = createstore(reducer);
export default store;
定義派發action的檔案(引入store.js和actioncreator.js),actions.js:
import store from "./store";
import actioncreator from "./actioncreator"
let actions =
store.dispatch(act);
}, removeaction(id)
store.dispatch(act);
}, changeaction(id)
}export default actions;
元件內繫結事件執行新增的action:
import react, from 'react';
import actions from "../store/actions";
class input extends component
input(e)
} render()
}export default input;
redux 初步理解
派發乙個 action 給 reducer,reducer 生成了乙個新的 state redux 通過 store 來儲存資料,store.getstate 獲得資料,而要更新 state,則需要 store.dispatch action 由於reducer 裡才會生成乙個新的 state,所以...
redux的初步實現
redux與vuex一致,在這裡由store index.js定義需求,通過dispatch將需求發給reducer,具體什麼時候發,由頁面 html vue jsx 來觸發 react與vue一樣,都存在父傳子 子傳父 兄弟元件之間的傳值,單純的頁面 之間傳值的實現方式需要掌握是肯定的,比如 父傳...
redux教程(一) 理解redux
react的頁面應用越來越複雜,資料和狀態的管理也越來越複雜,甚至夾雜著ajax非同步請求對資料狀態的變更。不知從什麼時候開始 react框架的資料狀態的管理已經越來越難以控制和 redux就是針對難以處理的資料管理,提出的一種解決方案。它能夠讓你的頁面的資料管理更輕鬆。當問到為什麼要使用redux...