React Native 检查StackNavigator的Render方法?

dtcbnfnu  于 2023-03-24  发布在  React
关注(0)|答案(1)|浏览(121)

每次我运行我的代码,我收到这个错误“警告:React.jsx:类型无效--应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。
检查StackNavigator的渲染方法。”
该代码应该显示一个登录页面
我做错了什么?

应用程序js

//import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import React, { useState, useEffect } from 'react';
import { firebase } from './config';

//import HomeScreen from './Screens/Home'
//import SettingsScreen from './Screens/Settings'
import Login from "./Screens/Login";
import Registration from "./Screens/Registration";
import Dashboard from "./Screens/Dashboard";

import Header from "./comp/Header";


//const Drawer = createDrawerNavigator();
const Stack = createStackNavigator();


  function App() 
  {
    const [initializing, setinitializing] = useState(true);
    const [user, setUser] = useState();

    // Handle user state changes 
    function onAuthStateChanged(user) {
      setUser(user);
      if (initializing) setinitializing(false)
    }

    useEffect(() => {
      const subscriber = firebase.auth().onAuthStateChanged(onAuthStateChanged);
      return subscriber;
    }, []);

    if (initializing) return null;

    if (!user) {
      return (
        <Stack.Navigator>
        <Stack.Screen
            name="Login"
            component={Login}
            options={{
              headerTitle: () => <Header name="Steven McWilliams " />,
              headerStyle: {
                height: 150,
                borderBottomLeftRadius: 50,
                borderBottomRightRadius: 50,
                backgroundColor: '#00e4d0',
                shadowColor: '#000',
                elevation: 25
              }
            }}
          />
          
          <Stack.Screen
            name="Registration"
            component={Registration}
            options={{
              headerTitle: () => <Header name="Steven McWilliams " />,
              headerStyle: {
                height: 150,
                borderBottomLeftRadius: 50,
                borderBottomRightRadius: 50,
                backgroundColor: '#00e4d0',
                shadowColor: '#000',
                elevation: 25
              }
            }}
          />
        </Stack.Navigator>
      );
    }

    return (
      <Stack.Navigator>
        <Stack.Screen
            name="Dashboard"
            component={Dashboard}
            options={{
              headerTitle: () => <Header name="Dashboard " />,
              headerStyle: {
                height: 150,
                borderBottomLeftRadius: 50,
                borderBottomRightRadius: 50,
                backgroundColor: '#00e4d0',
                shadowColor: '#000',
                elevation: 25
              }
            }}
          />
      </Stack.Navigator>
    );
  }
        
export default () => {
  return (
    <NavigationContainer>
      <App />
    </NavigationContainer>
  )
}

Header.js

import React, { Component } from 'react';
import { View, Text } from 'react-native';

class Header extends Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }

  render() {
    return (
      <View>
        <Text> Header </Text>
      </View>
    );
  }
}

export default Header;
tcomlyy6

tcomlyy61#

我会把导航分开

const loginStack = () => {
 return(
        <Stack.Navigator>
        <Stack.Screen
            name="Login"
         
            options={{
              headerTitle: () => <Header name="Steven McWilliams " />,
              headerStyle: {
                height: 150,
                borderBottomLeftRadius: 50,
                borderBottomRightRadius: 50,
                backgroundColor: '#00e4d0',
                shadowColor: '#000',
                elevation: 25
              }
            }}
          />

          <Stack.Screen
            name="Registration"
            component={Registration}
            options={{
              headerTitle: () => <Header name="Steven McWilliams " />,
              headerStyle: {
                height: 150,
                borderBottomLeftRadius: 50,
                borderBottomRightRadius: 50,
                backgroundColor: '#00e4d0',
                shadowColor: '#000',
                elevation: 25
              }
            }}
          />
        </Stack.Navigator>
      );
}

const mainStack = () => {
return(
<Stack.Navigator>
        <Stack.Screen
            name="Dashboard"
            component={Dashboard}
            options={{
              headerTitle: () => <Header name="Dashboard " />,
              headerStyle: {
                height: 150,
                borderBottomLeftRadius: 50,
                borderBottomRightRadius: 50,
                backgroundColor: '#00e4d0',
                shadowColor: '#000',
                elevation: 25
              }
            }}
          />
      </Stack.Navigator>
);
}

export default function App(){
 const [initializing, setinitializing] = useState(true);
    const [user, setUser] = useState();

    // Handle user state changes 
    function onAuthStateChanged(user) {
      setUser(user);
      if (initializing) setinitializing(false)
    }

    useEffect(() => {
      const subscriber = firebase.auth().onAuthStateChanged(onAuthStateChanged);
      return subscriber;
    }, []);

if(initializing){
return <ActivityIndicator />
}

return(
 <NavigationContainer>
    {
   user ? mainStack() : loginStack() // call the functions don't use them as components
}
 </NavigationContainer>
)
}

相关问题