Reducer中的动作分配不会更改存储状态。一切似乎都是正确的。控制台响应说减速器已设置为action.options。但是不改变状态。 如果将action.options等同于数字或字符串,那么一切都很好。商店正在改变。我该怎么办?
这是我的操作文件:
reducer.js
import { SENSOR_UI, SENSOR_UI_LENGHT, SENSOR_FETCHED, FONT_LOADED, SENSOR_BUTTON, Routes, EAction } from './types';
const initialState = {
isLoading: false,
fontLoaded: false,
deviceList: {},
};
const initialStateRoutes =
{
isLoading: false,
option: [],
}
const apiState = {
login: "",
password: "",
}
export function apiReducer (state = initialState, action){
switch (action.type) {
case 'SENSOR_BUTTON':
console.log("sdsdsddfsdfsdfsd");
return { ...state, sensor: action.sensor };
case 'SENSOR_FETCHED':
return { ...state,
deviceList: action.deviceList,
isLoading: true,
};
case 'FONT_LOADED':
return { ...state,
deviceList: action.deviceList,
fontLoaded: true,
};
case 'SENSOR_FAILED':
return { ...state, err: action.err };
default:
return state;
}
}
export function auth(state = apiState, action) {
switch (action.type) {
case EAction.FORM_AUTH_LOGIN_UPDATE:
return {
...state,
login : action.login
};
case EAction.FORM_AUTH_PASSWORD_UPDATE:
return {
...state,
password : action.password
};
case EAction.FORM_AUTH_RESET:
return {
...state,
login : "",
password : ""
};
case EAction.FORM_AUTH_AUTOFILL:
return {
...state,
login : action.login,
password : action.password
};
default:
return state;
}
}
export function routes(state=initialStateRoutes, action)
{
switch (action.type) {
case Routes.HOME:
return {
...state,
login : action.login
};
case Routes.DETAIL:
console.log("success dispath", action.options);
return {
...state,
sensor : action.sensor,
option: action.options,
isLoading: true
};
case Routes.Profile:
return {
...state,
profileInfo : action.profileInfo,
};
default:
return state;
}
}
actionCreator.js
import { merge } from 'react-lodash';
import {
SEARCH_CHANGE,
SENSOR_FAILED,
SENSOR_FETCHED,
FONT_LOADED,
SET_SENSOR,
EAction,
SENSOR_BUTTON,
Routes,
} from './types';
import data from '../config.json';
export function errorFetched(err) {
return { type: SENSOR_FAILED, err };
}
export function setSensorAction(successSensor) {
return {
type: SET_SENSOR,
sensor: successSensor,
};
}
export function deviceList(success) {
return { type: SENSOR_FETCHED, deviceList: success };
}
export function fontLoaded() {
return { type: FONT_LOADED };
}
export function sensorPress(sensor) {
return { type: SENSOR_BUTTON, sensor: sensor };
}
const cookies = {};
export function loginUpdate(event) {
return {
type: EAction.FORM_AUTH_LOGIN_UPDATE,
login: event,
};
}
export function passwordUpdate(event) {
return {
type: EAction.FORM_AUTH_PASSWORD_UPDATE,
password: event,
};
}
export function reset() {
return {
type: EAction.FORM_AUTH_RESET,
};
}
export function tryAutoFill() {
return function(dispatch) {
if (
cookies &&
cookies.login !== undefined &&
cookies.password !== undefined
) {
dispatch({
type: EAction.FORM_AUTH_AUTOFILL,
login: cookies.login,
password: cookies.password,
});
}
};
}
function auth(route) {
return NavigationActions.navigate({
routeName: route,
});
}
export function submit() {
return function(dispatch, getState) {
const state = getState();
if (state.app.login == 'admin' && state.app.password == '123') {
return dispatch(auth('Home'));
} else return dispatch(reset());
};
}
export function onBackPress() {
return function(dispatch, getState) {
const state = getState().nav;
if (state.index === 0) {
return false;
}
dispatch(NavigationActions.back());
return true;
};
}
function info(userInterface, key) {
const sensorGraph = {};
const option = userInterface.application.map.rx;
for (let x = 0; x < key.length; x++) {
const info = [];
const empty = [];
for (let i = 0; i < option.length; i++) {
for (let j = 0; j < option[i].options.length; j++) {
if (option[i].options[j] == key[x]) {
info.push(option[i]);
sensorGraph[key[x]] = info;
}
}
if (option[i].options[0] == undefined) {
empty.push(option[i]);
sensorGraph['info'] = empty;
}
}
}
return sensorGraph;
}
function getDetail(sensor, options) {
return {
type: Routes.DETAIL,
options,
sensor: sensor,
};
}
export function showDetailSensor(sensor) {
const config = ['batteryFlag', 'csettingsFlag', 'periodFlag', 'graphFlag'];
return function(dispatch, setState) {
let option = info(sensor, config)
dispatch(auth('Details'));
return dispatch(getDetail(sensor, option))
};
}
index.js
import { createStore, applyMiddleware, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import { initialState, auth, apiReducer, routes } from './reducer';
import { navReducer, middleware } from '../screens/AppNavigator';
import logger from 'redux-logger';
const appReducer = combineReducers({
app: auth,
nav: navReducer,
api: apiReducer,
rou: routes,
});
export const store = createStore(
appReducer,
applyMiddleware(middleware, thunk, logger)
);
答案 0 :(得分:0)
通常,解决方案很简单。我没事我使用了记录器,并试图捕获一个错误。但是事实证明,记录器本身是有错误的,我不知道它有什么问题