reactjs - 无法将数据传递给子反应组件
问题描述
我对 React 很陌生,所以如果我的问题很愚蠢,我很抱歉,但我被困在这里。我正在尝试将数据从父组件发送到子组件
家长:
export default function Dashboard() {
const [msg, setMSG] = useState<string>("Hello!");
return (
<Table message={msg}/>
);
}
孩子:
export default function Orders(message: string) {
const classes = useStyles();
return (
<React.Fragment>
{message}
</React.Fragment>
);
}
我收到一个错误:
输入'{消息:字符串;}' 不可分配给类型“字符串”。
解决方案
您似乎遇到了类型不匹配的问题 - 这意味着传递给您的子组件的是object
Type '{ message: string; }'
而您的子组件已通过执行此操作将其输入指定为单个string
-Orders(message: string)
尝试以下方法,保留类型检查:
// now `props` is of type object,
// with a `key` called `message`, the value of which is of type `string`
export default function Orders(props: {message: string}) {
const classes = useStyles();
return (
<React.Fragment>
{props.message}
</React.Fragment>
);
}
推荐阅读
- visual-studio - 在 Visual Studio 2017 中构建搅拌机?
- vb.net - 对 Google 自定义搜索 API 使用 VB GET 请求;未找到 URL 或非法请求
- sql - 更改列值(实际上没有在数据库中更新它)并将多行列为一行上的列值
- python - 预期条件间歇性失败并出现错误:TypeError: find_element() 采用 1 到 2 个位置参数,但给出了 3 个
- ruby-on-rails - 生产 Heroku(rails 应用程序)上的“未知协议”Bonsai Elasticsearch
- c# - 如何将 IObjectAdapter 与 JsonPatch 一起使用?
- jquery - 如何使用 JQuery 更改 HTML 表格元素
- tensorflow - 为什么我的模型会一直学习预测 4 个对象?
- javascript - 从 Excel O365 而不是 Google Docs for Gooogle Visualization API 中提取数据
- r - 计算每个列变量的相对丰度