简介
React Redux框架可以用来对React Native进行数据流管理。Redux是一个用于UI布局框架的标准库。Redux 是一个有用的架构,但不是非用不可。如果你不知道是否需要 Redux,那就是不需要它。如果你不知道是否需要 Redux,那就是不需要它。
安装
npm install --save redux
多数情况下还要下载
npm install --save react-redux
React Redux有几个基本概念:Store、Action、Reducer。
Store
就是保存数据的地方,
通过来包裹项目布局。
import App from './App'
import { Provider } from 'react-redux'
import store from './redux/store'
render(
<Provider store={store}>
<App />
</Provider>
)
Action
Action 是把数据从应用(传到 store 的有效载荷。它是 store 数据的唯一来源。一般来说你会通过 store.dispatch() 将 action 传到 store。
定义一个函数来生成 Action
function addTodoWithDispatch(text) {
const action = {
type: ADD_TODO,
text
}
dispatch(action)
}
Reducer
Reducer 是一个函数,它接受 Action 和当前 State 作为参数,返回一个新的 State。
const reducer = function (state, action) {
// ...
return new_state;
};
参考资料
- https://cn.redux.js.org/docs/basics/Store.html
- https://cn.redux.js.org/docs/basics/Action.html
- https://cn.redux.js.org/docs/basics/Reducers.html
- http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_usages.html