如何响应@aws-amplify/ui-react中auth状态的变化

How to respond to changes in auth state in @aws-amplify/ui-react

使用旧的 aws-amplify-react,为了在成功登录后将用户重定向到请求的 URL,我有:

<Authenticator
  onStateChange={authState => {
    if (authState === 'signedIn') {
      const { from } = this.props.location.state || {
        from: {
          pathname: '/',
          search: ''
        }
      };
      this.props.history.replace(from.pathname + from.search);
    }
  }}
/>

来自 documentation for the new version (@aws-amplify/ui-react) I see how to access the auth stateuseAuthenticator 挂钩,但没有内置工具来处理此状态中的更改。是否不受支持,我现在需要实施自己的变更检测?

什么都没有改变钩子useAuthenticator可以returnroute表示当前authState:

const { route } = useAuthenticator(context => [context.route]);

if (route === "authenticated"){
   
    const location = useLocation();
    const history = useHistory();

    const { from } = location.state || { from: { pathname: '/', search: '' } }; 
    history.replace(from.pathname + from.search);
}