【发布时间】: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