【问题标题】:Send to child this.props with change of one props更改一个道具发送给孩子 this.props
【发布时间】:2017-06-04 06:35:30
【问题描述】:

有什么办法可以发送来自父母的任何道具,但有一个道具改变了?

我有一些页面,我正在渲染InputList 组件。在这个组件中,我再次渲染InputList。所以我要发送 {...this.props} 并更改一个道具 - modal。但是当我发送<InputList modal={true} {this.props}/>modal 值取自 ...this.props。所以值是false 而不是true,有什么提示吗?

当然,有可能单独发送所有道具,但我有很多道具,所以我想使用 this.props.. 或者我可以使用 const { modal, ...others} = this.props 之类的东西,但还有更多道具,我我正在使用所以我有const { modal,...,...,...,...,...} = this.props

    class Page extends Component{
       render(){
         return(
              <InputList modal={false} list={...} valueA={..} valueB={..} 
                    firstTitle={..} secondTitle={..} />
         )
       }
    }

    class InputList extends Component{
       render(){
          const {modal,list,valueA,valueB,firstTitle,secondTitle} = this.props;
            ....
         return(
            ....
              <InputList modal={true} {...this.props} />
         )
       }
    }

【问题讨论】:

    标签: reactjs components parent-child render


    【解决方案1】:

    这对你有用吗?

    class InputList extends Component {
      render(){
        const passProps = Object.assign({}, this.props, {modal: true})
        return(
          ....
          <InputList {...passProps} />
        )
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 2017-06-03
      相关资源
      最近更新 更多