Open debbygigigi opened 5 years ago
reference:
https://ithelp.ithome.com.tw/articles/10186447 https://medium.com/@smile2gether/react-redux-todo-list-ccf63b622339 https://medium.com/frochu/%E9%80%81%E8%AE%93%E4%BD%A0%E7%9A%84action%E8%83%BD%E4%BD%9C%E6%9B%B4%E5%A4%9A-redux-thunk-c07bc5488e48
https://www.youtube.com/watch?v=1w-oQ-i1XB8 https://www.youtube.com/watch?v=AslncyG8whg
延續上一篇 react-app,直接改寫成加入 redux 的版本
安裝所需套件
連結 React 和 Redux
要讓 React 和 Redux 連結有兩種方式 第一種是
connect()
第二種是provider
Provider 把整個 App 包起來
App.js
reducer
建立 reducers/index.js
這邊 combind 所有 reducers
reducers/todoReducer.js
action
action/type.js
action/todoAction.js
改成 arrow function
回到 reducer,新增一個 case
action/todoActions.js
跟 component 結合 用到 connect()
components/todoList.js
在
componentWillMount
這個階段呼叫 fetchTodos 注意的是,這裡的方式是用 props 取得但是 store 裡的 state 如果要和 component 結合 就要使用 mapState
補上 props type
add redux devtool
Redux devtool https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd
安裝後會發現 redux 只出現
No store found. Make sure to follow the instructions.
這時要稍微修改一下 store.js
增加了 compose
SOP
先新增一個 type
todoAction.js
在要執行 function 的檔案
reference