store.getstate()评估为未定义

时间:2019-06-30 07:59:14

标签: react-native redux state redux-form

在本机项目中,我不断遇到此错误: 状态未定义,请评估store.getstate()

// store.js

 const composeEnhancers =
          typeof window === "object" &&                     window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({})
    : compose;

const enhancer = composeEnhancers(applyMiddleware(thunk));



const Store = createStore(
  combineReducers(
    {
      form: formReducer,
      appointmentsReducer
    },
    enhancer
  )
);
console.log(Store.getState());

export default Store;`

// reducer.js

import {
    FETCH_APPOINTMENTS_BEGIN,
    FETCH_APPOINTMENTS_SUCCESS,
    FETCH_APPOINTMENTS_FAILURE
  } from '../actions/appointmentsAction';

  const initialState = {
    data: [],
    loading: false,
    error: null
  };

  export default function appointmentsReducer(state = initialState, action) {
    switch(action.type) {
      case FETCH_APPOINTMENTS_BEGIN:

        return {
          ...state,
          loading: true,
          error: null
        };

      case FETCH_APPOINTMENTS_SUCCESS:

        return {
          ...state,
          loading: false,
          data: action.payload.appointments
        };

      case FETCH_APPOINTMENTS_FAILURE:

        return {
          ...state,
          loading: false,
          error: action.payload.error,
          data: []
        };

      default:

        return state;
    }
  }

// actions.js

import { listUrl } from "../cst";

export const FETCH_APPOINTMENTS_BEGIN = "FETCH_APPOINTMENTS_BEGIN";
export const FETCH_APPOINTMENTS_SUCCESS = "FETCH_APPOINTMENTS_SUCCESS";
export const FETCH_APPOINTMENTS_FAILURE = "FETCH_PRODUCTS_FAILURE";

export const fetchAppointmentsBegin = () => ({
  type: FETCH_APPOINTMENTS_BEGIN
});

export const fetchAppointmentsSuccess = appointments => ({
  type: FETCH_APPOINTMENTS_SUCCESS,
  payload: { appointments }
});

export const fetchAppointmentsFailure = error => ({
  type: FETCH_APPOINTMENTS_FAILURE,
  payload: { error }
});

export function fetchAppointments() {
  return dispatch => {
    dispatch(fetchAppointmentsBegin());
    return fetch(listUrl)
      .then(handleErrors)
      .then(res => res.json())
      .then(json => {
        dispatch(fetchApointmentsSuccess(json.appointment));
        return json.appointment;
      })
      .catch(error => dispatch(fetchAppointmentsFailure(error)));
  };
}

// Handle HTTP errors since fetch won't.
function handleErrors(response) {
  if (!response.ok) {
    throw Error(response.statusText);
  }
  return response;
}

// app.js

导出默认功能App(){

  return (
    <Provider store={Store}>
      <Navigation  />
    </Provider>
  );
}

//列出rendrer组件:

const mapStateToProps = state => ({
  data: state.appointments.data,
  loading: state.loading,
  error: state.error
});

store.getstate()的console.log给出:

Object {
  "appointmentsReducer": Object {
    "data": Array [],
    "error": null,
    "loading": false,
  },
  "form": Object {},

我不确定问题出在哪里。 是否由于异步调用处理不当? 如果我使用saga处理提取操作,是否可以解决问题? 任何帮助将不胜感激。

0 个答案:

没有答案