我正在查询用户以前是否登录过。我希望根据结果让他们转到不同的页面。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页。
1条答案
按热度按时间bvuwiixz1#
尝试此更改
至