首页 > 解决方案 > 尝试使用 Axios 获取时出现 getState 错误

问题描述

我正在尝试使用 axios 获取 JSON,我已经创建了 .action

returnToWorkQuestion.action.js

import axios from 'axios';
import {endpointURL} from '../../config/endpointConfig';
import {store} from '../rootStore';

export const returnToWorkQuestions = (email, questionCategory) => {
  let state = store.getState();
  console.log(state);
  return axios
    .get(`${endpointURL}getquestionnaire`, {
      params: {
        email: email,
        questionCategory: questionCategory,
      },
    })

    .then(response => {
      console.log(response);
    });
};

现在我只是想做一个console.log来看看它是否像这样读取

主页.js

import {returnToWorkQuestions} from '../../redux/actions/returntoworkquestion.action';
.
.
.
const dispatch = useDispatch();
  const fecthReturntoWorkQuestion = () => {
    dispatch(returnToWorkQuestions());
  };
.
.
.
<TouchableOpacity
            onPress={() => fecthReturntoWorkQuestion()}
            style={{
              backgroundColor: '#533AED',
              borderRadius: 10,
              shadowColor: '#000',
              shadowOffset: {
                width: 0,
                height: 2,
              },
              shadowOpacity: 0.25,
              shadowRadius: 1.84,
              elevation: 5,
            }}>
            <View style={{flexDirection: 'row'}}>
              <Image
                source={require('../../assets/images/empathoMask.png')}
                style={{flex: 0.3}}
              />

              <Text
                style={{
                  color: 'white',
                  flex: 0.6,
                  fontSize: 14,
                  fontWeight: '400',
                  letterSpacing: 2,
                  textAlign: 'center',
                  alignSelf: 'center',
                }}>
                COMPLETE WORKPLACE HEALTH SCREENING
              </Text>
            </View>
          </TouchableOpacity>

rootStore.js

import {combineReducers, createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {persistStore, persistReducer} from 'redux-persist';

//Reducers
import {AuthenticationReducer} from './reducers/authentication.reducer';
import {LoadingModalReducer} from './reducers/loadingmodal.reducer';
import {ReturnToWorkQuestions} from './reducers/returntoworkquestion.reducer';

const persistConfig = {
  key: 'root',
  storage: AsyncStorage,
  whitelist: ['user'],
};

const rootReducer = combineReducers({
  Authentication: persistReducer(persistConfig, AuthenticationReducer),
  LoadingModal: LoadingModalReducer,
  //ReturnToWorkQuestions: ReturnToWorkQuestions,
});

const store = createStore(rootReducer, applyMiddleware(thunk));

export const persistor = persistStore(store);

export default store;

但是当我运行它时,我收到错误“无法读取未定义的属性(读取'getState'),我在这里找不到解决方案,我该如何解决?

如果您需要另一段代码,请告诉我

标签: react-nativeaxios

解决方案


如前所述,您导出的商店不是命名的导出。

如果您导出一个对象,export default您必须像这样导入它import store from '../rootStore';

或者您将导出从更改export default store;export {store};


推荐阅读