簡體   English   中英

React js 父子數據傳遞

[英]React js Pass data from parent to child

我不知道為什么我不能將我在父組件上設置的值傳遞給子組件,當然獲取或獲取記錄沒有錯,我的代碼有什么問題嗎?

import ChildComponent from "../components/Forms/ChildComponent";

const [showchild, setchild] = useState(false);
const [getData, setData] = useState(false);
useEffect(() => {
   ...
   setchild(true)
   setData(data) //the data is from my database and it has a record
   ...
})

return(

    {showchild ? (
      <>
        <ChildComponent getData={getData}></ChildComponent>
      </>
    )}
)

子組件.js

export default function ChildComponent(getData) {

  useEffect(() => {
    console.log("get Data from Parent component: ", getData) // I dont know why I cant pass the value that i set on may parent component to child component,
  
  })

}
.....,

您可以使用以下方式訪問getData值:

解決方案 1

export default function ChildComponent({getData}) {

  useEffect(() => {
    console.log("get Data from Parent component: ", getData) 
  })

}

方案二

export default function ChildComponent(props) {

  const { getData } = props 
  useEffect(() => {
    console.log("get Data from Parent component: ", getData) 
  })

}

暫無
暫無

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

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