【问题标题】:passing data between child and parent component in nested component react js在嵌套组件反应js中在子组件和父组件之间传递数据
【发布时间】:2021-06-30 13:43:57
【问题描述】:

我使用一个组件作为子组件如下,在父组件即ShowBodyQuestionsForArchive组件中,我使用子组件如下:{children}

在 ShowBodyQuestionsForArchive 组件(父)中我有另一个组件如下:

我想改变ShowComparetiveItemsForArchive组件(子)中ShowImage组件的数据,显示为{children}

【问题讨论】:

标签: javascript reactjs nested components pass-data


【解决方案1】:
 const ShowComparativeQuestionForArchive = ({question, number, items}) => {
     changeShowImage(){
        //changedData
     }
       return(
          <ShowBodyQuestionForArchive question = {question} number = {number}> 
               <ShowComparativeItemsForArchive changeShowImage = {changeShowImage()} number = {number} items = {items} />
               {
                showImage ? <ShowImage {changedData} /> //example
               }
          <ShowBodyQuestionForArchive/>
       )
    }

现在调用 ShowComparativeItemsForArchive 中的 prop (changeShowImage)

【讨论】:

  • 可更改状态在 ShowBodyQuestionForArchive 组件中定义。当我使用 {children} 时,有没有办法可以更改此组件中的父属性?
  • React.Children 我认为这可能会有所帮助
猜你喜欢
  • 2020-10-26
  • 1970-01-01
  • 2017-03-14
  • 2023-04-03
  • 2017-09-04
  • 2020-03-04
  • 1970-01-01
  • 2020-07-08
  • 2018-01-02
相关资源
最近更新 更多