我正试图弄清楚如何有条件地传播道具。下面,我在第{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)
}
}
答案 0 :(得分:2)
更改
{this.props.isAuthenticated && {...this.props}}
到
{...(this.props.isAuthenticated && this.props)}
会这样做。