javascript - 在 reactjs 中将组件作为 prop 传递
问题描述
我认为这很简单,但看起来我做错了什么
const name = "Mike"
...
<MyComponent
data={
<Picture /> + " " + name
}
/>
const Picture () => return <img src="..." />
const myComponent (props) => return props.data
我得到这个输出
[object Object] Mike
谢谢你!
解决方案
const Picture = () => <img alt="" src="" />;
const MyComponent = props => props.data;
export default function App() {
return (
const user = ...
<MyComponent
data={
<>
<Picture />
{user.name}
</>
}
/>
);
}
任何值都应该通过{your value}
{user.name}
在这部分代码中,您不应该使用 returnconst MyComponent = props => props.data;
如果您想以经典方式返回它,请这样写:
const MyComponent = props => {
return props.data
};
推荐阅读
- python-3.x - ImportError:无法从部分初始化的模块“looker_dash_app.application.datatable”导入名称“init_dashboard”(循环导入)
- node.js - NodeJs Socket 部署在 AWS LoadBalancer 上不起作用
- php - 为什么即使在 docker-compose.yml 文件中指定了 MySQL 映像,也不会创建它?
- python-3.x - 按照字符串格式打印熊猫数据框列
- scala - 我们如何使用 spark 加载机器学习模型 .sav 文件
- javascript - 如何从特定的猫头鹰轮播中隐藏猫头鹰点?
- python - x 轴和 y 轴上的数据不均匀
- flutter - 小部件中的 double.infinity 和 double.maxFinite 有什么区别
- vega - Vega 堆积条形图 - 如何添加总计标签
- amazon-cognito - 用户池的 CognitoIdentityCredentials 相当于什么?