javascript - 为什么在传递道具时需要使用扩展运算符
问题描述
下面是代码:
const Info = (props) => (
<div>
<h1>Info</h1>
<p>The info is: {props.info}</p>
</div>
);
const withAdminWarning = (WrappedComponent) => {
return (props) => (
<div>
<p>This is private info. Please don't share!</p>
<WrappedComponent {...props}/> // why need to use spread operator?
</div>
);
};
const AdminInfo = withAdminWarning(Info);
ReactDOM.render(<AdminInfo info="There are the details" />, document.getElementById('app'));
我不明白的是,为什么必须这样:
<WrappedComponent {...props}/>
而且我不能将对象传递为:
const temp = {info: "There are the details"}
<WrappedComponent temp/>
那 {...props} 不也是一个对象吗?
解决方案
您不需要使用扩展运算符,您也可以执行类似的操作
const temp = { info: "There are the details }
<WrappedElement temp={temp} />
您可以稍后访问它props.temp.info
推荐阅读
- python - 用于从 dict 中提取所有 url 的正则表达式,如字符串
- vba - 错误 5941 VBA Word 2016 - 保存文件,从表中获取名称
- ios - 从 Android 迁移到 iOS AWS IoT
- sql-server - Powershell、DataTable、ExecuteReader、查询返回的零行
- c# - ASP.NET Web 项目模板包含 2 个用于导入 Microsoft.WebApplication.targets $VSToolsPath) 与 $(MSBuildExtensionsPath32) 的条目
- spring-mvc - 如何在 Spring Data REST 中创建 RESTful 查询语言
- php - 为什么在调用包装短代码时,短代码内的 wp_video_shortcode 会回显?
- python - 在 spark sql SELECT 查询中执行 .show() 时出错
- magento2 - 有什么方法可以为 Magento 2 中的所有简单产品创建自定义选项?
- python - 从 django 1.6 到 1.11:AppRegistryNotReady 异常