如何在两个钩子(Hook)组件中传递 Prop ?
我有一个使用 Modal
的根组件,我需要将 props title
传递给我的 Modal
组件并在组件中显示:
<Modal title="test" />
在模态组件中,我接收 props 作为参数:
const Modal = (title) => {
return (
<h6>{title}</h6>
)
但我收到的是未定义的消息。
我也尝试过使用 {}:
const Modal = ({title}) => {
为什么?
最佳答案
需要在子组件中引用props对象:
const Modal = (props) => {
return (
<h6>{props.title}</h6>
)
或者通过解构:
const Modal = ({title}) => {
return (
<h6>{title}</h6>
)
关于reactjs - 将 props 传递给 hook 组件变得未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60671546/