首页 > 解决方案 > 有没有办法在解构对象时访问对象本身?

问题描述

作为一个很好的例子,考虑这样的反应组件:

const MyComponent = (props) => {}

在上面的组件中,我们可以像这样访问 props 的孩子:

const MyComponent = ({count, value, ...rest}) => {}

并且使用 ...rest,我们也可以有未列出的孩子。

我的问题是,有没有办法在使用和不使用道具的情况下访问所有孩子?像这样的东西:

const MyComponent = ({count, value } = props) => { // I know this is wrong, it's just an example 
     return <>{props.count} {value} {props.someThingNotListed} </>  
     // getting able to access to destructed child direclty or by using props if we want
}

标签: javascriptreactjs

解决方案


推荐阅读