我有一個(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>);
如何在 React 中將 prop 傳遞給 {children}?
函數(shù)式編程
2023-07-14 15:35:55