javascript - Reactjs 功能组件默认 props 和 props 对象
问题描述
在功能组件中,我们可以像这样传递道具。
function Country(props){
}
我们可以像这样传递默认的道具值
function Country({name:'Sri Lanka',description:'blah! blah!'}){
}
然后我们可以将这样的道具传递给组件。
<Country name={'Germany'} description={'haha...'} />
但我确实想访问道具的历史对象。所以我做了
function Country({name:'Sri lanka',description:'haha',...props}){
}
但是当我console.log
props
它是一个空对象时。在这里,我想我可能也想传递props
对象。我怎么做?
<Country name={'Germany'} description={'haha...'} />
解决方案
当您使用 Country 组件时,您只需要传递道具。
像这样:
<Country name={'Germany'} description={'haha...'} {...props} />
推荐阅读
- javascript - 在 IIS 上的 iisnode 中使用 envi 变量时,为什么我无法获取当前登录用户?
- c# - 使用 Owin 进行 FluentValidation 忽略 Asp.Net WebApi2 中的 [FromUri] Dtos
- reactjs - 基于道具的动态标签
- sql - 使用 JSON 配置 PostgreSQL 以过滤复合条件
- django - 过滤后从多个表中呈现 Django 模板上的表中的数据
- java - 使用 RequestBuilder 发布文件时出错,403 Forbidden cloudflare
- express - 中间件在上一页不起作用
- excel - 从单元格中提取最后一个子字符串
- postgresql - Liferay 7,Hikari-Pool 连接不可用错误,发生在生产环境中,但不在预生产环境中
- discord - Discord.js 在线会员数量不会立即更新