簡體   English   中英

如何用另一個 React 組件替換一個 React 組件?

[英]How to replace a React Components with another React Components?

我有這樣的東西

<Box>
   <SomethingA>
</Box

我想將Box.SomethingA替換為Box.SomethingB

<Box>
   <SomethingB>
</Box>

我該怎么做?

我猜你想在事件發生后替換它,例如: button click 你可以試試這個:

function App(){
  const [box, changeBox] = useState(true)

  return(
    <div>
      <button onClick={()=>{changeBox(!box)}} >Click</button>
      <Box>
        { box ? <SomethingA/> : <SomethingB/>}
      </Box>
    </div>
  )
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM