我在 React Native 方面沒有那么有經驗,我正在開發我的應用程式,但我遇到了這個錯誤,但我不知道如何解決它。請幫忙。
以下是我的部分代碼:
const HomeScreen = ({ route, navigation }) => {
const [{ ios, appSettings, rtl_support }, dispatch] = useStateValue();
const [categoryData, setCategoryData] = useState({ 0:
route.params.data });
const [currentCategory, setCurrentCategory] = useState([]);
const [loading, setLoading] = useState(false);
const [bottomLevel, setBottomLevel] = useState(false);

uj5u.com熱心網友回復:
您沒有將資料引數傳遞給主螢屏組件,這就是它提到它為未定義的原因。
您必須以這種方式使用您的代碼才能忽略錯誤
const HomeScreen = ({ route, navigation }) => {
const { data } = route.params; // Here data prop can be undefined if you not pass it to this component as parameter
// navigation.navigate('HomeScreen', {data: 'whatever'})
const [{ ios, appSettings, rtl_support }, dispatch] = useStateValue();
const [categoryData, setCategoryData] = useState({ 0: data });
const [currentCategory, setCurrentCategory] = useState([]);
const [loading, setLoading] = useState(false);
const [bottomLevel, setBottomLevel] = useState(false);
}
更新
如果這是第一個螢屏,那么您不需要使用路由。
const HomeScreen = ({ navigation }) => {
const [{ ios, appSettings, rtl_support }, dispatch] = useStateValue();
const [categoryData, setCategoryData] = useState({ 0: 'whatever here' });
const [currentCategory, setCurrentCategory] = useState([]);
const [loading, setLoading] = useState(false);
const [bottomLevel, setBottomLevel] = useState(false);
}
我希望你明白了!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/428805.html
標籤:反应式
