AWS Amplify:onStatusChange然后呈现主页 [英] AWS Amplify: onStatusChange then render main page

查看:107
本文介绍了AWS Amplify:onStatusChange然后呈现主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当身份验证状态发生更改时,我试图呈现应用程序的主入口点,但是当我尝试转到应用程序的主入口点时,我得到一个空白屏幕.我假设如果未在render函数本身中调用主页,则无法进入主页?如果是这样,建立登录后如何呈现应用程序的主页面?

I am trying to render the main entry point of my application when an auth status change occurs but when I do try to go to the main entry point of my application I get a blank screen. I'm assuming I can't go to a main page if it isn't being called within the render function itself? If so, how do I render the main Page of my app when I established signIn?

class App extends Component {

  /*
  constructor(props) {
    super(props);
    this.state = {
        authState: null,
        authData: null
    }
}
*/
handleAuthStateChange(state) {
  alert(state)
  if (state === 'signedIn') { 
    alert('here');
    return ( // go To Entry point of app
      <ApolloProvider store={store} client={client}>
      <AppWithNavigationState/>
      </ApolloProvider>
    );
  }
}

    render() {
      //const { authState, authData } = this.state;
     // const signedIn = (authState === 'signedIn');


        return (
         <Authenticator hideDefault={true} onStateChange={this.handleAuthStateChange}>
        <Login/>
        </Authenticator>

      );
    }

}
export default App = codePush(App);

Login.js

export default class Login extends Component {


    render() {

       const { authState, onStateChange } = this.props;

       if (!['signIn', 'signedOut', 'signedUp'].includes(authState)) {
           alert('login')
        return null;
       }


        return (<View style={styles.container}>

            <View style={styles.backgroundContainer}>
            <Image source={images.icons.LoginImage} style={styles.backgroundImage} />
            </View>
            <View style={styles.overlay}>
            <Button iconLeft light rounded style={styles.facebookLoginButton}>
            <Icon style={styles.facebookIcon} name='logo-facebook' />
            <Text style={styles.facebookButtonText}>Login with Facebook</Text>
            </Button>
            <Button rounded bordered light style={styles.loginLaterButton}
            onPress={() => onStateChange('signedIn')}>
            <Text style={styles.buttonText}>Sign In Test</Text>
            </Button>


            </View>

            </View>

        );
      }
    }

推荐答案

这实际上与渲染和状态有关(与AWS Amplify无关).首先,在构造函数中设置状态:

This is really about rendering and state (and not anything to do with AWS Amplify). First, set up state in your constructor:

constructor(props) { super(props); this.state = { authState: '' }; }

constructor(props) { super(props); this.state = { authState: '' }; }

然后,您的onAuthStateChange()变为:

onAuthStateChange(newState) { if (newState === 'signedIn') { this.setState({ authState: newState }); } }

onAuthStateChange(newState) { if (newState === 'signedIn') { this.setState({ authState: newState }); } }

最后,在您的render()方法中,您可以调整渲染以使其根据您的身份验证状态执行正确的操作".

Finally, in your render() method, you adjust your rendering so that it does "the right thing" based on your auth state.

render() { if (this.state.authState === 'signedIn') { return (<ApolloProvider ...><MyApp/></ApolloProvider>); } else { return (<Authenticator .../>); } }

render() { if (this.state.authState === 'signedIn') { return (<ApolloProvider ...><MyApp/></ApolloProvider>); } else { return (<Authenticator .../>); } }

您也可以使用HOC将其抽象化(与AWS Amplify的withAuthenticator() HOC一样).基本上,身份验证器最初会显示出来.收到signedIn状态后,内部组件状态就会更新,这会导致该组件与应用程序的其余部分一起重新呈现.

You can abstract this away with a HOC as well (the same way the withAuthenticator() HOC from AWS Amplify does it). Basically, the Authenticator gets displayed initially. Once the signedIn state is received, the internal component state is updated and that causes a re-render of the component with the rest of your app.

这篇关于AWS Amplify:onStatusChange然后呈现主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆