Reactjs 反应组件:重新指定道具值

Reactjs 反应组件:重新指定道具值,reactjs,ecmascript-6,Reactjs,Ecmascript 6,我已经创建了一个自定义组件: <Button randomProps="asdasds" primary className="m-2" /> <Button label="secondary" secondary /> 现在,在我的Buutton.js中,我想将label的值设置为来自randomProps的值(当然,如果定义了rando

我已经创建了一个自定义组件:

      <Button
        randomProps="asdasds"
        primary
        className="m-2"
      />
      <Button
        label="secondary"
        secondary
      />

现在,在我的Buutton.js中,我想将label的值设置为来自randomProps的值(当然,如果定义了randomProps)。 所以,我做了这样的事情:

const Button = ({ label, randomProps }) => {  
    if (randomProps) label = randomProps;
    return (
    <FlatButton
      label={label}     
    />
  );
}
const按钮=({label,randomProps})=>{
如果(randomProps)标签=randomProps;
返回(
);
}

但是,我有一个ES6错误:赋值给函数参数“label”:没有参数重新赋值。

您不应该给收到的道具赋值。避免做类似于
label=randomProps
的事情

您可以创建另一个变量,或直接将该值用作三元运算符:

const Button = ({ label, randomProps }) => {
    return (
    <FlatButton
      label={randomProps? randomProps: label }
    />
  );
}
const按钮=({label,randomProps})=>{
返回(
);
}

是的,我喜欢你的想法,但现在我得到了:“对默认赋值不必要地使用条件表达式(没有不必要的三元组)”,所以我认为label={randomProps | | label}更好。选中此处并返回与测试相同的属性时,它相当于
randomProps | | label