简体   繁体   English

是否可以在 function 中使用道具作为默认值(使用反应/解构分配规则时)

[英]Is it possible to use props as a default value in a function (when using react/destructuring-assignment rule)

I have a function that getting val as a parameter and I want to set this.props.val as default value.我有一个 function 将val作为参数,我想将this.props.val设置为默认值。

My current function:我目前的 function:

foo(val = this.props.val) {
    console.log(val);
  }

I'm getting the follow lint error:我收到以下 lint 错误:

Must use destructuring props assignment (react/destructuring-assignment)必须使用解构道具分配(react/destructuring-assignment)

Is it possible to use props as a default value in a different way that will stand in the react/destructuring-assignment rule?是否有可能以不同的方式将 props 用作默认值,以符合react/destructuring-assignment规则?

Maybe there is some trick similar to the following?也许有一些类似于以下的技巧?

foo({ val } = this.props) {
    console.log(val);
  }

In the example above:在上面的例子中:

foo(5);// val = undefined
foo({val:5});// val = 5
foo();//val = this.props.val

Expected result预期结果

foo(5);// val = 5
foo({val:5});// val = {val:5}
foo();//val = this.props.val

[ I can use [ 我可以用

foo(val) {
    const { val: valProps } = this.props;
    const newVal = val || valProps;
    console.log(newVal);
  }

but I'm trying to find a cleaner way]但我正试图找到一种更清洁的方式]

It looks like you are trying to mix the type of the argument which function receives (sending integer or object) which is not the usual way to define function's parameters.看起来您正在尝试混合 function 接收的参数类型(发送 integer 或对象),这不是定义函数参数的常用方法。 I don't think you can handle such case in function's signature directly, but you could do something like this:我认为您不能直接在函数签名中处理这种情况,但是您可以执行以下操作:

this.props = {x: 1, y: 2, val: 3}

const foo = ({ val = 'MY_DEFAULT_VALUE' } = this.props) => {
  console.log(val);
}

foo(5);  // val = MY_DEFAULT_VALUE
foo({ val: 5 });  // val = 5
foo();  //val = this.props.val

Alternatively, if you really have to support both types as an argument (integers and objects), you should handle the argument you received within the function itself, something similar to:或者,如果您确实必须支持两种类型作为参数(整数和对象),您应该处理在 function 本身中收到的参数,类似于:

this.props = {x: 1, y: 2, val: 3}

const foo = (arg = this.props) => {
  if (arg.val) {  // Check if `arg` has a `val` property (meaning that `arg` is an object)
    console.log(arg.val);
  } else {  // `arg` is not an object with `val` property, assumed we got integer or empty param
    console.log(arg);
  }

  // Or even shorter:
  newVal = arg.val || arg;
  console.log(newVal);
}

foo(5);  // val = 5
foo({ val: 5 });  // val = 5
foo();  //val = 3 (from `this.props.val`)

You are getting a lint error which isn't exactly an error since it isn't related to runtime but is just an opinionated best practice relating to code style.您收到一个 lint 错误,这并不完全是错误,因为它与运行时无关,而只是与代码样式相关的自以为是的最佳实践。 Your code seems fine with respect to javascript.就 javascript 而言,您的代码似乎很好。 What you can do is, ignore the rule for this specific line by putting this comment above your function, // eslint-disable-line react/destructuring-assignment or disable this rule entirely in your eslint config which totally depends on your code style.您可以做的是,忽略此特定行的规则,将此注释放在您的 function, // eslint-disable-line react/destructuring-assignment或完全在您的 eslint 配置中禁用此规则,这完全取决于您的代码样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM