reactjs - 将道具传递给未定义的钩子组件
问题描述
我如何在两个钩子组件中传递道具?
我有一个使用 a 的根组件Modal
,我需要将Modal
道具传递给我的组件title
并在组件中显示:
<Modal title="test" />
在模态组件中,我接收道具作为参数:
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>
)
推荐阅读
- javascript - 输入文件为空时不运行函数
- function - 数据类型上的 Haskell 多态性
- java - 有没有办法使用仅凭据而不是 keytabs 的 java 客户端 API 连接到 kerberized 服务
- python - Python 在保存或加载 numpy 数组后被杀死
- azure - 参数为空或空 - 开始作业
- jwt - 接收消费请求通知并发送消费信息
- python - Assign values as keys within loop
- javascript - 如何在 React/Typescript 中定义历史和匹配类型(来自 React Router Dom)
- r - 使用 MICE 进行多重插补,使用 tidyLPA 进行潜在轮廓分析
- java - 如何使用 Firebase 实时数据库创建特定用户所有已保存帖子的列表?