我有一个 Public 和 Authenticated 函数,我用它来控制 react router v4 的身份验证,有人向我指出我应该React.createElement(component, { ...props, loggingIn, authenticated })
在<Route />
. 我不确定我应该如何或为什么要这样做。请解释和显示?
const Public = ({ loggingIn, authenticated, component, ...rest }) => (
<Route
{...rest}
render={(props) => {
return !authenticated ?
(React.createElement(component, { ...props, loggingIn, authenticated })) :
(<Redirect to="/" />);
}}
/>
);
这应该对您有用,因为您已经有一个<Public />
要在此处呈现的组件。将 更改component
为Component
考虑它是用户定义的元素。
这是来自 React 文档User-Defined Components Must Be Capitalized。
const Public = ({ loggingIn, authenticated, component: Component, ...rest }) => (
<Route
{...rest}
render={(props) => {
return !authenticated ?
(<Component loggingIn={ loggingIn } authenticated={ authenticated } { ...rest } />) :
(<Redirect to="/" />);
}}
/>
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句