我正在尝试弄清楚如何有条件地传播 Prop 。下面,我在 {this.props.isAuthenticated && {...this.props}}
行上收到错误,指出与 this 中的
:this
相关的意外标记.props.isAuthenticated1
class ProtectedRoute extends Component {
render() {
const ComponentToRender = this.props.component,
RouteToRender = (
<Route
{this.props.isAuthenticated && {...this.props}}
render={({Component}) =>
(this.props.isAuthenticated ? (<ComponentToRender {...this.props} />) :
(<Redirect
to={{
pathname: '/login',
state: {from: this.props.location
}}}
/>))}
/>)
return (RouteToRender)
}
}
最佳答案
改变
{this.props.isAuthenticated && {...this.props}}
至
{...(this.props.isAuthenticated && this.props)}
会做的。
关于javascript - 基于条件检查传播 props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44340035/