【发布时间】:2021-04-19 01:51:27
【问题描述】:
我有一个使用 React Router 的 React 应用程序。我的代码中有一个错误,这是由于我的自定义 ProtectedRoute 组件中的某些问题:
const ProtectedRoute = ({ component, ...args }) => (
<Route
component={withAuthenticationRequired(component, {
onRedirecting: () => <Loading />,
})}
{...args}
/>
);
export default ProtectedRoute;
您可以在此处详细阅读我的另一篇文章:Auth0 ProtectedRoute component preventing component from changing with state
但现在我只是想重写此函数以使用 render 道具而不是 component 道具(我认为这是导致问题的原因)。这是我尝试过的,但没有成功:
const ProtectedRoute = ({component: ComponentToRender, path, rest}) => {
return <Route
{...rest}
path={path}
render={(props) => {
withAuthenticationRequired(() => (<ComponentToRender {...props}/>), {
onRedirecting: () => <Loading/>
})
}
}
/>
}
有谁知道如何重写它以使用 render 属性?
谢谢!
【问题讨论】:
标签: javascript reactjs react-router react-router-dom auth0