您可以使用withAuthenticationRequired高阶组件来确保只有在用户已登录并且传递了用户属性后才渲染组件。 以下是一个示例:
import { withAuthenticationRequired } from "@auth0/auth0-react";
class ExampleComponent extends React.Component {
render() {
return (
Hello {this.props.user.name}!
);
}
}
export default withAuthenticationRequired(ExampleComponent);
在此示例中,withAuthenticationRequired将确保只有在用户已登录并且“user”属性已传递给ExampleComponent时才渲染组件。 如果用户未登录,则会自动跳转到登录页面。