首页 > 解决方案 > 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...'} />

标签: javascriptreactjs

解决方案


当您使用 Country 组件时,您只需要传递道具。

像这样:

<Country name={'Germany'} description={'haha...'} {...props} />

推荐阅读