Daniel Döbereiner
Daniel Döbereiner

Reputation: 119

getState error when trying to do a get with Axios

I'm trying to get a JSON using axios, I have created the .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);
    });
};

and for now I'm just trying to do a console.log to see if it is reading like this

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;

but when I run it I get the error "Cannot read properties of undefined (reading 'getState'), I can't find a solution here, how can I solve it?

If you need another piece of code please just let me know

Upvotes: 0

Views: 149

Answers (1)

Michael Bahl
Michael Bahl

Reputation: 3649

As mentioned you exported store not as named export.

In case you export an object with export default You have to import it like this import store from '../rootStore';

Or your change the export from export default store; to export {store};

Upvotes: 1

Related Questions