Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm quite new on react native this is just a practice project. now I have module regarding on authentication of users and how will navigate dashboard via conditioning the set asyncstorage item if the storage filtered that this storage has true value the user who access will directly move his/her screen to the AuthenticatedDriverScreen.

My Question:

  1. How to navigate in a nested navigator?
  2. How to call the getItem storage item on Navigator Screens?

My Error:

The action 'NAVIGATE' with payload {"name":"BindedScreenComponent","params":{"screen":"DriverDashboard"}} was not handled by any navigator.

Do you have a screen named 'BindedScreenComponent'?

I have already coded some functionality like Navigators Screen, Login Screen Etc. on the next paragraph I will share to you guys the code in every screens. I hope you will understand guys what I mean.

Navigator.JS

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


const UnauthenticatedScreen = () => {

    return (
        <Stack.Navigator>
            <Stack.Screen
                name="Login"
                component={Login}
                options=
                {{
                    headerShown: false,
                }}
            />
            <Stack.Screen
                name="Registration"
                component={Register}
                options={{
                    headerStyle: {
                        backgroundColor: '#4ABDFF'
                    },
                    headerTitleStyle: {
                        color: '#fff',
                    },
                    headerTintColor: '#fff',
                }}

            />

            <Stack.Screen
                name="Privacy"
                component={PrivacyPolicy}
                options={{
                    headerStyle: {
                        backgroundColor: '#4ABDFF'
                    },
                    headerTitleStyle: {
                        color: '#fff',
                    },
                    headerTitle: 'Privacy Policy',
                    headerTintColor: '#fff',
                }}
            />

            <Stack.Screen
                name="RegistrationSuccess"
                component={RegistrationSuccess}
                options=
                {{
                    headerShown: false,
                }}
            />

            <Stack.Screen
                name="ForgotPassword"
                component={ForgotPassword}
                options={{
                    headerStyle: {
                        backgroundColor: '#4ABDFF'
                    },
                    headerTitleStyle: {
                        color: '#fff',
                    },
                    headerTitle: 'Forgot Password',
                    headerTintColor: '#fff',
                }}
            />
        </Stack.Navigator>
    )

}

const BindedScreenComponent = () => {
    return (
        <Bind.Navigator>
            <Bind.Screen
                name="DriverDashboard"
                component={DriverDashboard}

                options={{
                    headerStyle: {
                        backgroundColor: '#4ABDFF',
                    },
                    headerTintColor: '#fff',
                    headerTitleStyle: {
                        fontWeight: 'bold',
                    },
                    headerTitle: 'Driver Dashboard'

                }}
            />

            <Bind.Screen
                name="DriverProfile"
                component={DriverProfile}
                options={{
                    headerStyle: {
                        backgroundColor: '#4ABDFF'
                    },
                    headerTitleStyle: {
                        color: '#fff',
                    },
                    headerTitle: 'Profile',
                    headerTintColor: '#fff',
                }}
            />


        </Bind.Navigator>
    )
}

const AuthenticatedDriverScreen = () => {
    return (
        <Drawer.Navigator drawerContent={props => <CustomDriverDrawer {...props} />} initialRouteName="DriverDashboard">
            <Drawer.Screen
                name="DriverDashboard"
                component={BindedScreenComponent}
                options={
                    {
                        drawerLabel: 'Home',
                        drawerIcon: ({ focused, color, size }) => (
                            <Icon type="font-awesome" name="home" style={{ fontSize: size, color: color }} />
                        ),

                    }
                }
            />
        </Drawer.Navigator>
    )
}

class Navigator extends React.Component {
    
    constructor(props) {
        super(props);
        this.state = {
            phone_number:'',
            password:''
        }

        
    }

    render() {

        const isLogin = false

        // try {
        //     const value = await AsyncStorage.getItem('access_token');
        //     if (value !== null) console.log(value)
        //   } catch (error) { // Error retrieving data }

        return (
    
            <NavigationContainer>

                {isLogin ? <AuthenticatedDriverScreen /> : <UnauthenticatedScreen />}
            
            </NavigationContainer>
    
        )
    }
}

export default Navigator;

Login.JS

    const UserInfo = {phone_number:'09361876184', password: '123456'}


class Login extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            phone_number:'',
            password:''
        }
    }



    LoginSubmitFunction = async () => {

        const phone_number = this.state.phone_number;
        const password = this.state.password;

        if(UserInfo.phone_number === phone_number && UserInfo.password === password) {
            
            await AsyncStorage.setItem('userToken', '1');
            this.props.navigation.navigate('BindedScreenComponent',{screen:'DriverDashboard'});
            
        }else {
            alert('Username or Password is Incorrect!');
        }
    }

    render() {
        return (
            <SafeAreaView style={[styles.container, { backgroundColor: '#ffff' }]}>
                <StatusBar barStyle="dark-content" backgroundColor="#ffff" />
                <ScrollView>
                    <View style={{ marginTop: 60 }}>
                        <Text style={{ fontSize: 25, fontWeight: 'bold' }}>LOGO</Text>
                        <Text style={{ fontSize: 20, fontWeight: 'bold', marginTop: 30 }}>Login your credential</Text>
                    </View>
                    <View style={{ marginTop: 30 }}>
                        <TextInput 
                            onChangeText={(phone_number) => this.setState({phone_number})}
                            value={this.state.phone_number}
                            placeholder="+63" 
                            keyboardType='numeric'
                            style={{ height: 50, borderRadius: 5, paddingLeft: 20, borderColor: '#EEEEEE', borderWidth: 1, backgroundColor: '#EEEEEE' }}
                        />
                        <TextInput 
                            onChangeText={(password) => this.setState({password})}
                            value={this.state.password}
                            placeholder="Password" 
                            secureTextEntry={true}
                            style={{ marginTop: 20, borderRadius: 5, paddingLeft: 20, height: 50, borderColor: '#EEEEEE', borderWidth: 1, backgroundColor: '#EEEEEE' }}
                        />
                    </View>
                    <View style={{ marginTop: 30 }}>
    
                        <TouchableOpacity 
                            onPress={this.LoginSubmitFunction}
                            style={{ height: 70 }} >
                            <Text style={{ backgroundColor: '#4ABDFF', fontSize: 20, textAlign: 'center', padding: 10, borderRadius: 5, color: '#ffff' }}>Login</Text>
                        </TouchableOpacity>
                    </View>
                    <View style={{ marginTop: 0, alignItems: 'center' }}>
                        <Text onPress={() => this.props.navigation.navigate('ForgotPassword')} style={{ fontSize: 17 }}>Forgot Password?</Text>
                        <Text style={{ fontSize: 17, marginTop: 10 }}>Don't have an account yet? <Text style={{ textDecorationLine: 'underline', color: '#4ABDFF', fontWeight: 'bold' }} onPress={() => this.props.navigation.navigate('Registration')}>Register Here</Text></Text>
                    </View>
                </ScrollView>
    
            </SafeAreaView>
        )
    }
}

const styles = StyleSheet.create({
    container: { flex: 2, padding: 40 },
});

export default Login;

Output:

Output


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
425 views
Welcome To Ask or Share your Answers For Others

1 Answer

The action 'NAVIGATE' with payload {"name":"BindedScreenComponent","params":{"screen":"DriverDashboard"}} was not handled by any navigator.

Because your LoginScreen in UnauthenticatedScreen stack. But UnauthenticatedScreen not contain any screen has name "BindedScreenComponent".

With use isLogin variable to check login, You dont need use NAVIGATE.

 await AsyncStorage.setItem('userToken', '1');

userToken is key, and has value is '1' in AsyncStorage.

In your Navigator.JS, You can call it:

const isLogin = await AsyncStorage.getItem('userToken') === '1'

if isLogin = true, it return AuthenticatedDriverScreen . And else, return UnauthenticatedScreen


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...