Как передать состояние родительскому компоненту после входа в дочерний компонент реагировать родной - PullRequest
0 голосов
/ 16 марта 2020

Привет, я ищу решение для передачи состояния от дочернего компонента к родительскому, я застрял.

Я пытаюсь перейти с LoginScreen после нажатия кнопки на экране входа в систему для компонента приложения, который является стековой навигацией

Компонент стека навигации:

if (this.state.isLoading) {
        // We haven't finished checking for the token yet
        return <ActivityIndicator />;
    }

    return (
        <NavigationContainer>
            <Stack.Navigator
                screenOptions={{
                    headerShown: false
                }}
            >
                        {this.state.isLoggedIn == 0 ? (
                    // No token found, user isn't signed in
                    <Stack.Screen
                        name="SignIn"
                        component={LoginScreen}

                    />
                ) : (
                    <Stack.Screen name="Home" component={HomeScreen} />
                )}
            </Stack.Navigator>
        </NavigationContainer>
    );

И LoginScreen:

<KeyboardAvoidingView behavior={"padding"}
                                      keyboardVerticalOffset={
                                            Platform.select({
                                                ios: () => 70,
                                                android: () => 70
                                            })()
                                      }
                                      style={styles.bottomMain}
                >
                    <View style={styles.loginInput}>
                        <TextInput style={{margin: 10, fontSize: 20}}
                                   placeholder='Email'
                                   onChangeText={(email)=>this.setState({email})}
                                   textContentType='emailAddress'
                                   autoCapitalize='none'
                                   autoCorrect={false}
                        />
                    </View>
                    <View style={styles.passwordInput}>
                        <TextInput style={{margin: 10, fontSize: 20}}
                                   placeholder='Password'
                                   onChangeText={(password)=>this.setState({password})}
                                   secureTextEntry={true}
                                   textContentType='password'
                                   autoCapitalize='none'
                                   autoCorrect={false}
                        />
                    </View>
                    <View style={styles.buttonContainer}>
                        <TouchableOpacity onPress={()=>this.loginUser(this.state.email, this.state.password)}>
                            <View style={styles.loginButton}>
                                <Text>Zaloguj się</Text>
                            </View>
                        </TouchableOpacity>
                        <TouchableOpacity onPress={()=>this.signUpUser(this.state.email, this.state.password)}>
                            <View style={styles.registerButton}>
                                <Text>Zarejestruj się</Text>
                            </View>
                        </TouchableOpacity>
                    </View>

                </KeyboardAvoidingView>

У меня также есть asyncstorage, и это хранилище пытается войти в систему после запуска приложения, вы можете войти в систему с помощью asyncstorage. Но, нажав кнопку «Войти» на дочернем элементе из окна «LoginScreen», я не могу добавить sh в компонент приложения, в котором указано state.isLoggedIn = 1

.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...