如何从另一个类刷新React-Native中的主要App类渲染

时间:2019-03-27 08:19:34

标签: reactjs react-native

我正在尝试使用React-Native创建我的第一个应用程序, 我创建了一个呈现身份验证表单的类,在处理了提交后,应用程序应使用其选项卡呈现导航屏幕。我认为我可以从“身份验证”屏幕中以某种方式“刷新”应用程序类的呈现,以便它可以再次检查用户是否已通过身份验证,但是我不太确定

App.Js:

import AuthScreen from './screens/AuthScreen';

export default class App extends React.Component {
  state = {
    isLoadingComplete: false,
    isAuthenticated: false,
  };

  render() {
    if (!this.state.isLoadingComplete && !this.props.skipLoadingScreen) {
      return (
        <AppLoading
          startAsync={this._loadResourcesAsync}
          onError={this._handleLoadingError}
          onFinish={this._handleFinishLoading}
        />
      );
    } else {
      if(this.state.isAuthenticated == true) {
        return (
          <View style={styles.container}>
            <StatusBar hidden = {true} />
            <AppNavigator />
          </View>
        );
      } else {
        return (
          <View style={styles.container}>
            <StatusBar hidden = {true} />
            <AuthScreen />
          </View>
        );
      }
    }
  }

AuthScreen.js:

export default class AuthScreen extends Component {
  handleSubmit = () => {
    const value = this._form.getValue();
    console.log('value: ', value);
  }

  render() {
    return (
      <View style={styles.container}>
        <View style={styles.auth_container}>
          <Form
            ref={c => this._form = c}
            type={User}
            options={options}
          />
          <Button
            title="Submit"
            onPress={this.handleSubmit}
          />
        </View>
      </View>
    );
  }
}

1 个答案:

答案 0 :(得分:1)

您可以使用react-navigation(RN导航库)进行此操作。但是对于每个有问题的代码,您都试图在屏幕之间切换。

以您自己的方式:如果成功遵循AuthScreen的handleSubmit方法

handleSubmit = () => {
    check auth logic
    this.props.onSuccessFullLogin(value)
}

更新ParentComponent中的状态以在屏幕App Component之间切换:

<AuthScreen />应该像<AuthScreen onSuccessFullLogin={()=>this.setState({isAuthenticated:true})} />