基于条件检查传播道具

时间:2017-06-03 03:47:08

标签: javascript reactjs

我正试图弄清楚如何有条件地传播道具。下面,我在第{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)
  }
}

1 个答案:

答案 0 :(得分:2)

更改

{this.props.isAuthenticated && {...this.props}}

{...(this.props.isAuthenticated && this.props)}

会这样做。