javascript - 基于条件检查传播 props

标签 javascript reactjs

我正在尝试弄清楚如何有条件地传播 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/

相关文章:

javascript - 如何找出ReactJS中的错误来自哪里?

reactjs - 自定义过滤器 ag 网格 react

reactjs - 在 React 构建中减少 Lighthouse "First Meaningful Paint"

javascript - 在 Protractor 测试中设置 chromedriver 首选项

c# - 在 asp.net C# 页面中提醒

javascript - Foundation 4 Reveal 不起作用?

javascript - 用react.js类编写的BMI计算器需要在提交按钮上单击两次才能改变一些this.state值

javascript - 如何自动化使用Electron框架开发的桌面应用程序?

javascript - 如何访问函数中的变量

reactjs - 将 React 组件部署到许多个人项目中