【问题标题】:getState error when trying to do a get with Axios尝试使用 Axios 获取时出现 getState 错误
【发布时间】:2021-09-14 12:02:51
【问题描述】:

我正在尝试使用 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 来看看它是否像这样读取

Home.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'),我在这里找不到解决方案,我该如何解决?

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

【问题讨论】:

  • 这是由于 store 未定义import {store} from '../rootStore'; 可能是拼写错误或者它不是 rootStore 中的命名导出。
  • 刚刚更新了rootStore

标签: react-native axios


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 2022-01-05
    • 2016-11-23
    • 2021-06-02
    • 2019-08-04
    • 1970-01-01
    • 2022-11-07
    相关资源
    最近更新 更多