首页 > 解决方案 > 将道具传递给未定义的钩子组件

问题描述

我如何在两个钩子组件中传递道具?

我有一个使用 a 的根组件Modal,我需要将Modal道具传递给我的组件title并在组件中显示:

<Modal title="test" />

在模态组件中,我接收道具作为参数:

const Modal = (title) => {

return (
    <h6>{title}</h6>
)

但我收到未定义的。

我也试过 {} :

const Modal = ({title}) => {

为什么?

标签: reactjsreact-hooks

解决方案


需要在子组件中引用 props 对象:

const Modal = (props) => {

return (
    <h6>{props.title}</h6>
)

或通过解构:

const Modal = ({title}) => {

return (
    <h6>{title}</h6>
)

推荐阅读