根据条件检查传播道具
问题描述:
我想弄清楚如何有条件地传播道具。下面,我在this.props.isAuthenticated1
坐上说与this
意外的标记线{this.props.isAuthenticated && {...this.props}}
一个错误:根据条件检查传播道具
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)}
将做到这一点。