最佳答案
我正在尝试使用 React-Redux 库,我得到了标题上的错误。我用 Provider 包装了组件,但是只有在实现 useDispatch ()挂钩时才会出现错误。
这个应用程序工作得很好,直到我添加了 useDispatch ()行。关于调度函数的其余行可以删除,但我仍然得到相同的错误。
如果你能帮我,我会很感激的,谢谢
这是我的代码:
import 'react-native-gesture-handler';
import {NavigationContainer} from '@react-navigation/native';
import Navigator from './navigation/Navigator';
import React, {useEffect, useState, useCallback} from 'react';
import {SafeAreaView, StyleSheet, Text, View} from 'react-native';
import {createStore, combineReducers} from 'redux';
import {Provider, useDispatch} from 'react-redux';
import dataReducer from './store/reducers/dataReducer';
import {CONSTANTS} from './constants/constants';
import {saveInitialData} from './store/actions/dataActions';
const App = () => {
const [fetched, setFetched] = useState(initialState);
const dispatch = useDispatch();
const saveInitialDataHandler = useCallback(data => {
dispatch(saveInitialData(data));
callback;
}, []);
const rootReducer = combineReducers({
content: dataReducer,
});
const store = createStore(rootReducer);
useEffect(() => {
fetchData();
}, []);
const fetchData = () => {
fetch(CONSTANTS.database)
.then(response => response.json())
.then(responseJSON => {
setFetched(true);
saveInitialDataHandler(responseJSON);
});
};
if (!fetched) {
return (
<Provider store={store}>
<View stlye={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<Text></Text>
</View>
</Provider>
);
} else {
return (
<Provider store={store}>
<NavigationContainer>
<SafeAreaView style={styles.SafeAreaView}>
<Navigator></Navigator>
</SafeAreaView>
</NavigationContainer>
</Provider>
);
}
};
const styles = StyleSheet.create({
SafeAreaView: {flex: 1},
});
export default App;