React Native 在AuthStateChanged上响应本机Firebase不工作

wtzytmuj  于 2022-12-14  发布在  React
关注(0)|答案(1)|浏览(154)

我正在查询用户以前是否登录过。我希望根据结果让他们转到不同的页面。onChangeState不起作用

import { firebase } from './config';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

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

  function onAuthStateChanged(user) {
    setUser(user);
    if (initializing) setInitializin(false);
  }
  useEffect(() => {
    const subscriber = firebase.auth().onAuthStateChanged(onAuthStateChanged);
    return subscriber;
  }, []);
  if (initializing) return null;
  if (!user) {
    return (

      <NavigationContainer independent={true}>
        <Stack.Navigator>
          <Stack.Screen
            name='Login'
            component={LoginScreen}
            options={{ headerShown: false, headerBackVisible: false }}
          />
          <Stack.Screen
            name='Create'
            component={CreateAccount}
            options={{ headerShown: true, headerTitle: 'Create An Account' }} />
        </Stack.Navigator>
      </NavigationContainer>

    );
  } else if (user)
    return (
      <NavigationContainer independent={true}>
        <Stack.Navigator>
          <Stack.Screen
            name='Daily'
            component={DailyScreen}
            options={{ headerShown: false, headerBackVisible: false }} />
          <Stack.Screen
            name='Detail'
            component={Detail}
            options={{ headerShown: false, headerBackVisible: false }} />
        </Stack.Navigator>
      </NavigationContainer>
    ); return (
      <NavigationContainer>
        <App />
      </NavigationContainer>
    );
}
export default () => {
  return (
    <NavigationContainer independent={true}>
      <App />
    </NavigationContainer>
  )
}

如果用户以前登录过,我希望它转到DailyScreen页。但它总是转到LoginScreen页。

bvuwiixz

bvuwiixz1#

尝试此更改

const [user, setUser] = useState('');

const [user, setUser] = useState(null);

相关问题