首先创建项目
react-native init rn-redux
然后进入项目继续安装
yarn add react-redux redux redux-thunk
项目结构目录如下
在reducers文件夹中创建listReducer.js文件
import { ACTION_LIST_GET_LIST } from "../actions";
const initState = { data: [] }
const listReducer = (state = initState, action) => {
const newState = state;
// 判断 action 类型
switch (action.type) {
case ACTION_LIST_GET_LIST:
return {
...newState,
data: newState.data.concat(action.preload)
}
default:
return state;
}
};
export default listReducer;
然后在创建一个index.js文件,当多reducer的时候合并reducer,这里先写上
import { combineReducers } from "redux";
import listReducer from "./listReduce