【问题标题】:Is there a way to get arguments after distructuring?有没有办法在解构后获得论点?
【发布时间】:2021-09-22 01:42:59
【问题描述】:
我试图从 React 的功能组件内部获取参数列表。
export function SomeFunction ({ first, second third }){
// do something with the arguments
let props = this.arguments;
return <div> hello</div>}
那么在我解构它们之后,有没有办法得到所有的论点?
【问题讨论】:
标签:
reactjs
react-functional-component
【解决方案1】:
如何将解构移到第二行:
export function SomeFunction (props) {
const { first, second, third } = props;
//...
}
如果您需要引用整个 props 对象,可以通过props 进行。
【解决方案2】:
您很接近,您可以使用函数中的arguments 值。 arguments 是一个类似数组的对象,由于该函数只使用一个参数,因此您可以访问第零个元素。
function SomeFunction({ first, second, third }) {
// do something with the arguments
const props = arguments[0];
console.log(props);
return 42;
}
SomeFunction({ first: "1", second: 2, third: "third", fourth: "4th" });