We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redux 是 JavaScript 状态容器,提供可预测化的状态管理。Redux 由以下组件组成:
Redux 遵循的三个原则:
单一事实来源:整个应用的状态存储在单个 store 中的对象/状态树里。单一状态树可以更容易地跟踪随时间的变化,并调试或检查应用程序。
状态是只读的:改变状态的唯一方法是去触发一个动作。动作是描述变化的普通 JS 对象。就像 state 是数据的最小表示一样,该操作是对数据更改的最小表示。
使用纯函数进行更改:为了指定状态树如何通过操作进行转换,你需要纯函数。纯函数是那些返回值仅取决于其参数值的函数。
简要描述:
代码演示:
import { createStore } from "redux"; /* 创建reducer ** 可以使用单独的一个reducer,也可以将多个reducer合并为一个reducer,即:combineReducers() ** action发出命令后将state放入reucer加工函数中,返回新的state,对state进行加工处理 */ const reducer = (state = { counter: 0 }, action) => { switch (action.type) { case "INCREASE": return { counter: state.counter + 1 }; case "DECREASE": return { counter: state.counter - 1 }; default: return state; } }; /* 创建action ** 用户是接触不到state的,只能有view触发,所以,这个action可以理解为指令,需要发出多少动作就有多少指令 ** action是一个对象,必须有一个叫type的参数,定义action类型 */ const actions = { increase: () => ({ type: "INCREASE" }), decrease: () => ({ type: "DECREASE" }), }; /* 创建的store,使用createStore方法 ** store 可以理解为有多个加工机器的总工厂 ** 提供subscribe,dispatch,getState这些方法。 */ const store = createStore(reducer); // 订阅 const unsubscribe = store.subscribe(() => console.log(store.getState())); // 发起一系列 action store.dispatch(actions.increase()); // {counter: 1} store.dispatch(actions.increase()); // {counter: 2} store.dispatch(actions.increase()); // {counter: 3} store.dispatch(actions.decrease()); // {counter: 2} // 停止监听 state 更新 unsubscribe();
Redux 与 Flux 有何不同?
Redux 的优点如下:
关于 Redux 的资料:
React Redux 将组件区分为 容器组件 和 UI 组件:
两个核心:
Provider 顶层组件,目的是让所有组件都能够访问到 Redux 中的数据
connect:connect(mapStateToProps, mapDispatchToProps)(MyComponent)
connect(mapStateToProps, mapDispatchToProps)(MyComponent)
DEMO:实现计数器,完整 Demo 可以看这里。
学习资料:
我们使用 redux-thunk 在 React 中调用 API。因为 reduce 是纯函数,所以没有副作用,比如调用 API。
因此,我们必须使用 redux-thunk 从 Action creators 那里进行 API 调用。Action creator 派发一个 action,将来自 API 的数据放入 action 的 payload 中。Reducers 接收数据,其余的过程也是相同的。
redux-thunk 是一个中间件。一旦它被引入到项目中,每次派发一个 action 时,都会通过 thunk 传递。如果它是一个函数,它只是等待函数处理并返回响应。如果它不是一个函数,它只是正常处理。
举个栗子:sendEmailAPI 是从组件中调用的函数,它接受一个数据并返回一个函数,其中 dispatch 作为参数。我们使用 redux-thunk 调用 API apiservice,并等待收到响应。一旦接收到响应,我们就使用 payload 派发一个 action。
import apiservice from "../services/apiservice"; export function sendEmail(data) { return { type: "SEND_EMAIL", payload: data }; } export function sendEmailAPI(email) { return function(dispatch) { return apiservice.callAPI(email).then((data) => { dispatch(sendEmail(data)); }); }; }
其他中间件:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Redux
Redux 是 JavaScript 状态容器,提供可预测化的状态管理。Redux 由以下组件组成:
Redux 遵循的三个原则:
单一事实来源:整个应用的状态存储在单个 store 中的对象/状态树里。单一状态树可以更容易地跟踪随时间的变化,并调试或检查应用程序。
状态是只读的:改变状态的唯一方法是去触发一个动作。动作是描述变化的普通 JS 对象。就像 state 是数据的最小表示一样,该操作是对数据更改的最小表示。
使用纯函数进行更改:为了指定状态树如何通过操作进行转换,你需要纯函数。纯函数是那些返回值仅取决于其参数值的函数。
简要描述:
代码演示:
Redux 与 Flux 有何不同?
Redux 的优点如下:
组织 - Redux 准确地说明了代码的组织方式,这使得代码在团队使用时更加一致和简单。
关于 Redux 的资料:
React-Redux
React Redux 将组件区分为 容器组件 和 UI 组件:
两个核心:
Provider 顶层组件,目的是让所有组件都能够访问到 Redux 中的数据
connect:
connect(mapStateToProps, mapDispatchToProps)(MyComponent)
DEMO:实现计数器,完整 Demo 可以看这里。
学习资料:
Redux 中间件
我们使用 redux-thunk 在 React 中调用 API。因为 reduce 是纯函数,所以没有副作用,比如调用 API。
因此,我们必须使用 redux-thunk 从 Action creators 那里进行 API 调用。Action creator 派发一个 action,将来自 API 的数据放入 action 的 payload 中。Reducers 接收数据,其余的过程也是相同的。
redux-thunk 是一个中间件。一旦它被引入到项目中,每次派发一个 action 时,都会通过 thunk 传递。如果它是一个函数,它只是等待函数处理并返回响应。如果它不是一个函数,它只是正常处理。
举个栗子:sendEmailAPI 是从组件中调用的函数,它接受一个数据并返回一个函数,其中 dispatch 作为参数。我们使用 redux-thunk 调用 API apiservice,并等待收到响应。一旦接收到响应,我们就使用 payload 派发一个 action。
其他中间件:
学习资料:
The text was updated successfully, but these errors were encountered: