首页 > 解决方案 > Spread Operator ... 与 Material UI 中 Tab 的 props 中的函数调用一起使用

问题描述

<Tab label="Item One" {...a11yProps(1)} />在 Material UI 中看到了这一点,其中函数在道具中使用 ... 展开运算符调用当我尝试时

console.log(...a11yProps(3));

我得到错误,任何人都可以告诉这里发生了什么<Tab label="Item One" {...a11yProps(1)} />传播运算符如何与函数调用一起使用?

标签: javascriptreactjsmaterial-uireact-props

解决方案


演示


function fn(x, y, z) {
  console.log("fn() called with", x, y, z);
}
let x;

x = ["a", "b", "c"];


fn( ...x ); //function call with spread of an array

console.log("spreading into an object", { ...x } ); // spreading into an object clones the properties of the array

x = {a: 1, b: 2, c: 3};

console.log("spreading into an object", { ...x } ); // spreading object into an object clones it

fn( ...x ); //spreading an object into a function call is an error



function fb2(){
  return obj = {object : 'new Object'}
}

console.log({...fb2()}.object);


推荐阅读