Open gracekrcx opened 4 years ago
https://redux.js.org/basics/usage-with-react#implementing-container-components 這個範例 code 在 todos 這個 state 進入 component 之前,getVisibleTodos 這個 function 也做了一些邏輯判斷 -> 當 component 裡面要用的資料,跟在 redux store 裡的資料結構不一樣的時候 ( component 需要的是“ 看得到的 todo” 但 redux store 存的是所有的 todos,所以要先篩選)
Getting Started with Redux Dan 教你 redux
Redux 原始碼的註解寫法
/**
* Note that each middleware will be given the `dispatch` and `getState` functions
* as named arguments.
*
* @param {...Function} middlewares The middleware chain to be applied.
* @returns {Function} A store enhancer applying the middleware.
*/
第一次看 O'Reilly 書裡說 redux 其實只有 99 行程式碼 其中有一個 utils/isPlainObject.ts
/**
* @param obj The object to inspect.
* @returns True if the argument appears to be a plain object.
*/
export default function isPlainObject(obj: any): boolean {
if (typeof obj !== 'object' || obj === null) return false
let proto = obj
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
}
return Object.getPrototypeOf(obj) === proto
}
難道這就是優良的 code ???
A Predictable State Container for JS Apps
link
為什麼需要 redux
Three Principles
Single source of truth 唯一真相來源 The state of your whole application is stored in an object tree within a single store. 整個 application 的 state 都存在樹狀物件放在唯一的 store 裡面,redux 只有唯一一個 store 將所有的資料用物件的方式包起來。
State is read-only State 是唯讀的 The only way to change the state is to emit an action, an object describing what happened. 改變 state 的唯一的方式是發出一個 action,這個物件描述改變。
Changes are made with pure functions 變更由 pure function 執行 To specify how the state tree is transformed by actions, you write pure reducers. 要指定 state tree 如何藉由 action 來轉變,你必須撰寫 pure reducer。
store
整個 Redux 就是以 store 為核心來運作
const store = createStore(reducers, preloadedState, enhancer);
Returns (Store)store 方法
1. store.getState() 返回當前的 state。 它與 store 的最後一個 reducer 返回值相同。
2. store.dispatch(action) 分發 action。這是觸發 state 變化的惟一途徑。
3. store.subscribe(render) 向 store 訂閱 dispatch 事件,當 dispatch 發生時會執行訂閱的 callback (render)
connect 介紹
connect 是 'react-redux' 提供的 API
The connect() function connects a React component to a Redux store.
Dan 的 connect.js 在 global state 和 component 之間再多一層,這一層確認有哪些 state 會進入這個 component,有哪些 dispatch 跟這個 component 有關係。
redux <=> connect <=> react
action and action creator
Actions are like breadcrumbs of what has happened.
Action:一個 pure JavaScript object,描述發生的事件類別(type),以及所承載的資訊(payload)。 Action creator:幫助產生 Action 的 helper function
reducer
To tie state and actions together, we write a function called a reducer.
拿 old state 和 action 經過一些 logic 之後產生 new state 給 store reducer 裡面包含了 整理資料,處理資料的邏輯 action 資料進來後就看在 reducer 要怎麼改 reducer is pure function
reducer:產生新的 state,newState = reducer(prevState, action)
(previousState, action) => newState
combineReducers
Note that each of these reducers is managing its own part of the global state. The state parameter is different for every reducer, and corresponds to the part of the state it manages.
Container 和 Components
withRouter + redux
寫 redux 的小方法
先寫『讀資料』的部分,再去寫 『修改』的部分,把流程再切開,更清楚(initial state 先設定,再串聯給 container 可以收到 props,再寫前面的 dispatch)
讀資料 2 steps,update 4 steps,所以先把簡單的寫好
react 非同步呼叫 api
(扣除 api) redux 與使用者互動流程
react-redux
<Provider />
是 react-redux 中的組件 Provider Component 單純的把 store 用 context 傳下去,從最上層透過 context 傳整個 component tree,用 context 取代 props 來傳遞就可以在整個 component tree 中取用 context 而不用繁瑣的一層一層寫 props,不過要注意React.Children.only 的關係,Provider 只能有一個 Child Component<Provider />: Component providing the redux store
簡單說,react-redux 提供了 Provider,讓你把 store 注入到你的 react component 裡參考文章
使用 Angular4 + Redux 做個 TodoMVC Three Principles 三大原則 Redux 基礎概念 Redux 中文文档 Redex 核心概念筆記 從 source code 來看 React-Redux 怎麼讓 Redux 跟 React 共舞