简体   繁体   English

我在使用 useEffect 和 React Redux 时遇到问题。 我在 useEffect 中收到错误说“没有从渲染中返回”

[英]I am having an issue with useEffect and React Redux. I am getting errors within useEffect saying “nothing was returned from render”

I cannot tell why I am getting the error below.我不知道为什么我会收到以下错误。 I suspect it has to do with the provider but I do not know how to fix this.我怀疑它与提供商有关,但我不知道如何解决这个问题。 The reason I suspect it has to do with the provider is simply because when I comment out lines 71-73 and uncomment lines 67-69 it renders and the error goes away.我怀疑它与提供程序有关的原因仅仅是因为当我注释掉第 71-73 行和取消注释第 67-69 行时,它呈现并且错误消失了。

Error: Main(...): Nothing was returned from render.错误:Main(...):渲染没有返回任何内容。 This usually means a return statement is missing.这通常意味着缺少 return 语句。 Or, to render nothing, return null.或者,不渲染任何内容,返回 null。 Error:错误:

  40 | useEffect(() => {    
  41 |   setLoaded(true);
  42 |   firebase.auth().onAuthStateChanged(user => {
> 43 |     user ? setLoggedIn(true) : setLoggedIn(false);
     | ^  44 |   });
  45 |   // return() => null;
  46 | }, []);

My code:我的代码:

     1  import { StatusBar } from 'expo-status-bar';
     2  import React, { useState, useEffect } from 'react';
     3  import { NavigationContainer } from "@react-navigation/native";
     4  import { createStackNavigator } from "@react-navigation/stack";
     5  import { View, Text } from "react-native"
     6  import { Provider } from "react-redux"
     7  import { createStore, applyMiddleware } from "redux"
     8  import LandingScreen from "./components/auth/Landing"
     9  import RegisterScreen from "./components/auth/Register"
    10  import MainScreen from "./components/Main"
    11  import firebase from "firebase"
    12  import config from "./config"
    13  import rootReducer from "./redux/reducers"
    14  import thunk from "redux-thunk"
    15  
    16  const store = createStore(rootReducer, applyMiddleware(thunk))
    17  
    18  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    19  const firebaseConfig = {
    20    apiKey: config.API_KEY,
    21    authDomain: config.AUTH_DOMAIN,
    22    projectId: config.PROJECT_ID,
    23    storageBucket: config.STORAGE_BUCKET,
    24    messagingSenderId: config.MESSAGING_SENDER_ID,
    25    appId: config.APP_ID, 
    26    measurementId: config.MEASUREMENT_ID
    27  };
    28  
    29  if(firebase.apps.length === 0) {
    30    firebase.initializeApp(firebaseConfig);
    31  }
    32  
    33  const Stack = createStackNavigator();
    34  
    35  export function App() {
    36  
    37    const [ loaded, setLoaded ] = useState(false);
    38    const [ loggedIn, setLoggedIn ] = useState(false);
    39  
    40    useEffect(() => {    
    41      setLoaded(true);
    42      firebase.auth().onAuthStateChanged(user => {
    43        user ? setLoggedIn(true) : setLoggedIn(false);
    44      });
    45      // return() => null;
    46    }, []);
    47  
    48    if(!loaded) {
    49      return(
    50        <View style={{ flex: 1, justifyContent: "center" }}>
    51          <Text>Loading</Text>
    52        </View>
    53      );
    54    } 
    55  
    56    if(!loggedIn) {
    57      return (
    58        <NavigationContainer>
    59          <Stack.Navigator initialRouteName="Landing">
    60            <Stack.Screen name="Landing" component={LandingScreen} options={{ headerShown: false }}/>
    61            <Stack.Screen name="Register" component={RegisterScreen} options={{ headerShown: false }}/>
    62          </Stack.Navigator>
    63        </NavigationContainer>
    64      );
    65    } else {
    66      return (
    67        // <View style={{ flex: 1, justifyContent: "center" }}>
    68        //   <Text>jawn</Text>
    69        // </View>
    70        
    71        <Provider store={store}>
    72          <MainScreen />
    73        </Provider>
    74      );
    75    };
    76  
    77  }
    78  
    79  export default App

This might help这可能会有所帮助

if (!loaded) {
  return (...);
}

return !loggedIn ? (
  <NavigationContainer>
    <Stack.Navigator initialRouteName="Landing">
      <Stack.Screen
        name="Landing"
        component={LandingScreen}
        options={{ headerShown: false }}
      />
      <Stack.Screen
        name="Register"
        component={RegisterScreen}
        options={{ headerShown: false }}
      />
    </Stack.Navigator>
  </NavigationContainer>
) : (
  <Provider store={store}>
    <MainScreen />
  </Provider>
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我收到此错误:React Hook useEffect 内部的变量在每次渲染后都会丢失? - Why i am getting this error: variable from inside React Hook useEffect will be lost after each render? React Hook useEffect 缺少依赖项。 当没有任何东西损坏时,为什么我会收到此错误? - React Hook useEffect has a missing dependency. Why am I getting this error when nothing is broken? 反应:即使我要返回,渲染也没有返回任何内容 - React: Nothing was returned from render even though I am returning 在反应 useEffect 生命周期中,我得到了无限的结果 - in react useEffect life cycle, i am getting infinite result 渲染自动填充的搜索结果 Debounce 在 React 中不起作用,我无法在 useEffect 中调用我的 debounce - To render auto populated search result Debounce is not working in React and i am not able to call my debounce inside useEffect 我是否无法在功能性 React 组件中使用带有 useEffect 的内联“if”? - Am I unable to use inline “if” with useEffect in a functional React component? 在 React 本机 useEffect 中,使用异步调用我得到一个未定义的,在以下情况下如何避免未定义? - In React native useEffect ,using async call i am getting a undefined , How to avoid that undefined in the below case? React Navigation 说渲染没有返回任何内容 - React Navigation saying nothing was returned from render 我正在使用 UseEffect Hook,但组件仍然出现 Uncaught TypeError: Cannot set properties of null。 在反应 - I am using UseEffect Hook, but still component is getting Uncaught TypeError: Cannot set properties of null. in react 无法使用 react 和 redux 在待办事项列表中添加待办事项。 我究竟做错了什么? - Not able to add todo in a todo-list using react and redux. What am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM