Redux 中文文檔html
actions(一個 oject) --- 經過調用 dispatch 傳給 --->
reducers(接收 state 和 action,返回新的 state) --- 使用其返回值 --->
改變 store 中的 stateredux
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script src="https://unpkg.com/redux@4.0.1/dist/redux.min.js"></script> <script> // 1.建立action function add(content) { return { type: "ADD", content } } function del(content) { return { type: "DEL", content } } // 2. 建立執行action的reducer function list(state = [], action) { switch (action.type) { case "ADD": return [...state, action.content]; case "DEL": let i = state.findIndex(d => d == action.content); if (i > -1) { var t = [...state]; t.splice(i, 1); return t; } else { return state } default: return state } } const testApp = Redux.combineReducers({ list }) // 3. 根據reducer建立store let store = Redux.createStore(testApp); // 測試 console.log(store.getState()); store.dispatch(add('123')); store.dispatch(add('456')); store.dispatch(add('789')); console.log(store.getState()); store.dispatch(del('456')); console.log(store.getState()); </script> </body> </html>