Javascript Redux状态问题:TypeError:undefined不是对象
我对本地和redux特别不熟悉,我收到了一条错误消息,我真的很难处理 这是我遇到的错误 TypeError:undefined不是对象(0, _reactRedux.useSelector)(函数(状态){ 返回state.mybiges.mybiges; }).长度') 我将在这里向您展示我迄今为止获得的所有相关代码,然后解释我在这一切背后的意图 MainScreen.jsJavascript Redux状态问题:TypeError:undefined不是对象,javascript,react-native,redux,react-redux,Javascript,React Native,Redux,React Redux,我对本地和redux特别不熟悉,我收到了一条错误消息,我真的很难处理 这是我遇到的错误 TypeError:undefined不是对象(0, _reactRedux.useSelector)(函数(状态){ 返回state.mybiges.mybiges; }).长度') 我将在这里向您展示我迄今为止获得的所有相关代码,然后解释我在这一切背后的意图 MainScreen.js import React, {useState, useEffect} from 'react'; import {Vi
import React, {useState, useEffect} from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';
import { useDispatch, useSelector } from 'react-redux';
import CustomButton from '../components/CustomButton';
import CustomPigeonPicker from '../components/CustomPigeonsPicker';
import { addPigeon } from '../store/actions/pigeon';
const MainScreen = props =>{
dispatch = useDispatch();
const availablePigeons = (useSelector(state => state.myPigeons.myPigeons)).length;
return(
<View style={styles.screen}>
<View style={styles.tophalf}>
<CustomPigeonPicker style={styles.pigeonpicker}
placeholder={`You have so many pigeons: ${availablePigeons}`}
/>
</View>
<View style={styles.bottomhalf}>
<CustomButton
style={styles.button}
onPress={() => dispatch(addPigeon)}
/>
</View>
</View>
)
};
const styles = StyleSheet.create({
screen:{
flexDirection: "column",
flex: 1
},
button:{
fontFamily: "AmaticBold",
//Ab hier Einstellungen zum Schatten
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 5,
},
shadowOpacity: 0.34,
shadowRadius: 6,
elevation: 3.5,
width: "30%",
},
tophalf:{
flex: 1,
alignItems: "center"
},
bottomhalf:{
flex:1,
alignItems: "center"
},
pigeonpicker:{
}
});
export default MainScreen;
import pigeon from '../models/pigeon';
const ALLPIGEONS = [
new pigeon(
1,
"red",
"Red-billed pigeon",
" "
),
new pigeon(
2,
"blue",
"Blue pigeon",
" "
),
new pigeon(
3,
"white",
"Release dove",
" "
),
new pigeon(
4,
"brown",
"Brown cuckoo-dove",
" "
),
new pigeon(
5,
"green",
"Green pigeon",
" "
),
];
export default ALLPIGEONS;
//Automatic imports
import { StatusBar } from 'expo-status-bar';
import React, {useState} from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Provider } from 'react-redux';
//My imports
import * as Font from 'expo-font';
import {AppLoading} from 'expo';
import ReduxThunk from 'redux-thunk';
import { createStore, combineReducers, applyMiddleware } from 'redux';
//import other screens
import PBNavigator from './navigation/PBNavigator';
//import Reducers
import authReducer from './store/reducers/auth';
import pigeonReducer from './store/reducers/pigeon';
//Loading Fonts, returns promise
const fetchFonts = () => {
return Font.loadAsync({
'Magnus' : require('./assets/fonts/MagnusText.ttf'),
'AmaticBold' : require('./assets/fonts/Amatic-Bold.ttf'),
'AmaticRegular' : require('./assets/fonts/AmaticSC-Regular.ttf'),
'SEASRN' : require('./assets/fonts/SEASRN.ttf'),
});
};
const rootReducer = combineReducers({
auth: authReducer,
myPigeons: pigeonReducer,
});
const store = createStore(rootReducer, applyMiddleware(ReduxThunk));
export default function App() {
const [dataLoaded, setDataLoaded] = useState(false); //are fonts loaded?
if(!dataLoaded){ //will go into if clause because fonts are not loaded
return(
<AppLoading
startAsync={fetchFonts}
onFinish={() => setDataLoaded(true)}
onError={(err) => console.log(err)}
/>
)
}
return (
<Provider store={store}>
<PBNavigator/>
</Provider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
鸽子.js(减速器)
鸽子_data.js
import React, {useState, useEffect} from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';
import { useDispatch, useSelector } from 'react-redux';
import CustomButton from '../components/CustomButton';
import CustomPigeonPicker from '../components/CustomPigeonsPicker';
import { addPigeon } from '../store/actions/pigeon';
const MainScreen = props =>{
dispatch = useDispatch();
const availablePigeons = (useSelector(state => state.myPigeons.myPigeons)).length;
return(
<View style={styles.screen}>
<View style={styles.tophalf}>
<CustomPigeonPicker style={styles.pigeonpicker}
placeholder={`You have so many pigeons: ${availablePigeons}`}
/>
</View>
<View style={styles.bottomhalf}>
<CustomButton
style={styles.button}
onPress={() => dispatch(addPigeon)}
/>
</View>
</View>
)
};
const styles = StyleSheet.create({
screen:{
flexDirection: "column",
flex: 1
},
button:{
fontFamily: "AmaticBold",
//Ab hier Einstellungen zum Schatten
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 5,
},
shadowOpacity: 0.34,
shadowRadius: 6,
elevation: 3.5,
width: "30%",
},
tophalf:{
flex: 1,
alignItems: "center"
},
bottomhalf:{
flex:1,
alignItems: "center"
},
pigeonpicker:{
}
});
export default MainScreen;
import pigeon from '../models/pigeon';
const ALLPIGEONS = [
new pigeon(
1,
"red",
"Red-billed pigeon",
" "
),
new pigeon(
2,
"blue",
"Blue pigeon",
" "
),
new pigeon(
3,
"white",
"Release dove",
" "
),
new pigeon(
4,
"brown",
"Brown cuckoo-dove",
" "
),
new pigeon(
5,
"green",
"Green pigeon",
" "
),
];
export default ALLPIGEONS;
//Automatic imports
import { StatusBar } from 'expo-status-bar';
import React, {useState} from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Provider } from 'react-redux';
//My imports
import * as Font from 'expo-font';
import {AppLoading} from 'expo';
import ReduxThunk from 'redux-thunk';
import { createStore, combineReducers, applyMiddleware } from 'redux';
//import other screens
import PBNavigator from './navigation/PBNavigator';
//import Reducers
import authReducer from './store/reducers/auth';
import pigeonReducer from './store/reducers/pigeon';
//Loading Fonts, returns promise
const fetchFonts = () => {
return Font.loadAsync({
'Magnus' : require('./assets/fonts/MagnusText.ttf'),
'AmaticBold' : require('./assets/fonts/Amatic-Bold.ttf'),
'AmaticRegular' : require('./assets/fonts/AmaticSC-Regular.ttf'),
'SEASRN' : require('./assets/fonts/SEASRN.ttf'),
});
};
const rootReducer = combineReducers({
auth: authReducer,
myPigeons: pigeonReducer,
});
const store = createStore(rootReducer, applyMiddleware(ReduxThunk));
export default function App() {
const [dataLoaded, setDataLoaded] = useState(false); //are fonts loaded?
if(!dataLoaded){ //will go into if clause because fonts are not loaded
return(
<AppLoading
startAsync={fetchFonts}
onFinish={() => setDataLoaded(true)}
onError={(err) => console.log(err)}
/>
)
}
return (
<Provider store={store}>
<PBNavigator/>
</Provider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
App.js
import React, {useState, useEffect} from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';
import { useDispatch, useSelector } from 'react-redux';
import CustomButton from '../components/CustomButton';
import CustomPigeonPicker from '../components/CustomPigeonsPicker';
import { addPigeon } from '../store/actions/pigeon';
const MainScreen = props =>{
dispatch = useDispatch();
const availablePigeons = (useSelector(state => state.myPigeons.myPigeons)).length;
return(
<View style={styles.screen}>
<View style={styles.tophalf}>
<CustomPigeonPicker style={styles.pigeonpicker}
placeholder={`You have so many pigeons: ${availablePigeons}`}
/>
</View>
<View style={styles.bottomhalf}>
<CustomButton
style={styles.button}
onPress={() => dispatch(addPigeon)}
/>
</View>
</View>
)
};
const styles = StyleSheet.create({
screen:{
flexDirection: "column",
flex: 1
},
button:{
fontFamily: "AmaticBold",
//Ab hier Einstellungen zum Schatten
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 5,
},
shadowOpacity: 0.34,
shadowRadius: 6,
elevation: 3.5,
width: "30%",
},
tophalf:{
flex: 1,
alignItems: "center"
},
bottomhalf:{
flex:1,
alignItems: "center"
},
pigeonpicker:{
}
});
export default MainScreen;
import pigeon from '../models/pigeon';
const ALLPIGEONS = [
new pigeon(
1,
"red",
"Red-billed pigeon",
" "
),
new pigeon(
2,
"blue",
"Blue pigeon",
" "
),
new pigeon(
3,
"white",
"Release dove",
" "
),
new pigeon(
4,
"brown",
"Brown cuckoo-dove",
" "
),
new pigeon(
5,
"green",
"Green pigeon",
" "
),
];
export default ALLPIGEONS;
//Automatic imports
import { StatusBar } from 'expo-status-bar';
import React, {useState} from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Provider } from 'react-redux';
//My imports
import * as Font from 'expo-font';
import {AppLoading} from 'expo';
import ReduxThunk from 'redux-thunk';
import { createStore, combineReducers, applyMiddleware } from 'redux';
//import other screens
import PBNavigator from './navigation/PBNavigator';
//import Reducers
import authReducer from './store/reducers/auth';
import pigeonReducer from './store/reducers/pigeon';
//Loading Fonts, returns promise
const fetchFonts = () => {
return Font.loadAsync({
'Magnus' : require('./assets/fonts/MagnusText.ttf'),
'AmaticBold' : require('./assets/fonts/Amatic-Bold.ttf'),
'AmaticRegular' : require('./assets/fonts/AmaticSC-Regular.ttf'),
'SEASRN' : require('./assets/fonts/SEASRN.ttf'),
});
};
const rootReducer = combineReducers({
auth: authReducer,
myPigeons: pigeonReducer,
});
const store = createStore(rootReducer, applyMiddleware(ReduxThunk));
export default function App() {
const [dataLoaded, setDataLoaded] = useState(false); //are fonts loaded?
if(!dataLoaded){ //will go into if clause because fonts are not loaded
return(
<AppLoading
startAsync={fetchFonts}
onFinish={() => setDataLoaded(true)}
onError={(err) => console.log(err)}
/>
)
}
return (
<Provider store={store}>
<PBNavigator/>
</Provider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
这个代码看起来是错误的
var generatedbige=Math.floor(Math.random()*allbiges.length);
generatedbike.昵称=“Kuba”代码>此代码看起来有误
var generatedbige=Math.floor(Math.random()*allbiges.length);
generatedbike.昵称=“Kuba”在启动应用程序时,main.js中的code>state.mybiges未定义。如果试图对redux对象执行某些操作,则应在其中定义该值。最好的方法是在代码中始终进行防御检查,以检查对象是否“未定义”。main.js中的state.mybiges在启动应用程序时未定义。如果试图对redux对象执行某些操作,则应在其中定义该值。最好的方法是在代码中始终进行防御性检查,以检查对象是否“未定义”。在哪里声明MainScreen?在PB navigator中?在哪里声明主屏幕?在PB navigator中?很抱歉,这对我没有帮助,同样的错误仍然存在。我很确定这与redux以及我如何尝试访问存储数据有关,我只是不知道我在那里做错了什么。很抱歉,这对我没有帮助,同样的错误仍然存在,我很确定这与redux和我如何访问存储数据有关,我只是不知道我在那里做错了什么。听起来可能是对我问题的解释。你有没有建议我如何在应用程序启动后立即定义它,如果有,在哪些组件中它是最好的?基本上,我想有一个现有的初始状态,我的鸽子减缩,以便我可以读取它的价值。我很抱歉,但这也没有帮助我。据我所知,我的reducer中已经有一个初始状态,这也应该设置,因为我将它作为默认状态传递到我的reducer中,因此,我仍然无法弄清楚到底是什么导致了这个错误。我甚至在youtube上查看了几个关于redux状态管理的教程,我不明白为什么我的代码会以任何方式抛出和出错。就像我说的,我对这个话题还不太熟悉,还不是很熟练。为了诊断,你能评论一下它抱怨的那句话吗:state.mybiges.mybiges。相反,在state内打印值:state.mybiges.mybiges.听起来可能是对我的问题的一种解释。你有没有建议我如何在应用程序启动后立即定义它,如果有,在哪些组件中它是最好的?基本上,我想有一个现有的初始状态,我的鸽子减缩,以便我可以读取它的价值。我很抱歉,但这也没有帮助我。据我所知,我的reducer中已经有一个初始状态,这也应该设置,因为我将它作为默认状态传递到我的reducer中,因此,我仍然无法弄清楚到底是什么导致了这个错误。我甚至在youtube上查看了几个关于redux状态管理的教程,我不明白为什么我的代码会以任何方式抛出和出错。就像我说的,我对这个话题还不太熟悉,还不是很熟练。为了诊断,你能评论一下它抱怨的那句话吗:state.mybiges.mybiges。相反,在state:state.mybiges.mybiges.好的,MainScreen是StackNavigator的一部分,它本身也是SwitchNavigator的一部分。我在最后附加了PBNavigator文件,以便您可以查看我如何设置导航。是的,主屏幕是StackNavigator的一部分,而StackNavigator本身也是Switch Navigator的一部分。我在最后附加了PBNavigator文件,以便您可以查看我是如何设置导航的。