第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 React 中將 prop 傳遞給 {children}?

如何在 React 中將 prop 傳遞給 {children}?

函數(shù)式編程 2023-07-14 15:35:55
我有一個(gè)父組件,充當(dāng)其子組件的包裝器。如何將道具傳遞給將使用以下格式渲染的子項(xiàng)?import React, { useEffect, useState } from 'react';const Parent = ({ children }) => {  const [completeState, setCompleteState] = useState(false);  useEffect(    () => {      /* .. code that runs then sets completeState to true */    setCompleteState(true);     }, []  );  return (     <section>       /*           how to pass a 'didComplete' prop to children?           didComplete={completeState}       */       {children} // Child component below would be rendered here with the didComplete prop passed in    </section>  )}import React from 'react';const Child = ({ didComplete }) => (<h1>The function completed? {didComplete}</h1>); 
查看完整描述

1 回答

?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊

它props.children是一個(gè) React Element,它只不過(guò)是一個(gè)普通的 JS 對(duì)象,具有需要在 DOM 中呈現(xiàn)的內(nèi)容的描述。


為了提供其他詳細(xì)信息,我們需要克隆現(xiàn)有的 React Element 對(duì)象并提供其他詳細(xì)信息。


為此,我們可以使用React.cloneElementAPI 將附加屬性傳遞給children:


return (

     <section>

       {React.cloneElement(children, {didComplete: completeState})}

    </section>

);


查看完整回答
反對(duì) 回復(fù) 2023-07-14
  • 1 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)