Javascript React-Eslint解构赋值

Javascript React-Eslint解构赋值,javascript,reactjs,eslint,Javascript,Reactjs,Eslint,我正在使用eslint规则反应/解构分配,该规则将抛出。我如何分解它 getStuff(model = this.state.model) { // some stuff } 你的例子不是在分解结构。函数如下所示: function getStuff({ property1, property2 }) { ... } 上面的示例将从传递给给定函数的任何对象中提取property1和property2。您的示例不是在解构。函数如下所示: function getStuff({ prope

我正在使用eslint规则
反应/解构分配
,该规则将抛出。我如何分解它

getStuff(model = this.state.model) {
   // some stuff
}

你的例子不是在分解结构。函数如下所示:

function getStuff({ property1, property2 }) {
...
}

上面的示例将从传递给给定函数的任何对象中提取property1和property2。

您的示例不是在解构。函数如下所示:

function getStuff({ property1, property2 }) {
...
}

上面的示例将从传递给给定函数的任何对象中提取property1和property2。

ESLINT不允许使用
this.state.model
,它建议您使用解构:

const { model: stateModel } = this.state
getStuff(model = stateModel) {
但是,恐怕是在react类组件中。因此,您不能在方法定义之前定义常量或变量。您应该禁用该行的eslint:

getStuff(model = this.state.model) { // eslint-disable-line

或者,您也可以使用类似于:

 getStuff(model) {
   const { model: stateModel } = this.state
   const passedModel = model ? model : stateModel

   // then work through passedModel
}
getStuff(model) {
  const { model: stateModel } = this.state || { model: model }
  console.log(stateModel)

嗯,我能想到这个应该可以做到:

getStuff({state: {model}} = this) {
但是,坦率地说,我从来没有用过这样的方法。试着让我知道

更新

您也可以使用类似于:

 getStuff(model) {
   const { model: stateModel } = this.state
   const passedModel = model ? model : stateModel

   // then work through passedModel
}
getStuff(model) {
  const { model: stateModel } = this.state || { model: model }
  console.log(stateModel)
或者,简单地说:

getStuff(model) {
  const { model } = this.state || { model }
  // ----------------- similar to { model: model }
  console.log(model)

ESLINT不允许使用
this.state.model
,它建议您使用解构:

const { model: stateModel } = this.state
getStuff(model = stateModel) {
但是,恐怕是在react类组件中。因此,您不能在方法定义之前定义常量或变量。您应该禁用该行的eslint:

getStuff(model = this.state.model) { // eslint-disable-line

或者,您也可以使用类似于:

 getStuff(model) {
   const { model: stateModel } = this.state
   const passedModel = model ? model : stateModel

   // then work through passedModel
}
getStuff(model) {
  const { model: stateModel } = this.state || { model: model }
  console.log(stateModel)

嗯,我能想到这个应该可以做到:

getStuff({state: {model}} = this) {
但是,坦率地说,我从来没有用过这样的方法。试着让我知道

更新

您也可以使用类似于:

 getStuff(model) {
   const { model: stateModel } = this.state
   const passedModel = model ? model : stateModel

   // then work through passedModel
}
getStuff(model) {
  const { model: stateModel } = this.state || { model: model }
  console.log(stateModel)
或者,简单地说:

getStuff(model) {
  const { model } = this.state || { model }
  // ----------------- similar to { model: model }
  console.log(model)

正如我理解您的需要,您希望为来自state的模型提供一个默认var,并且如果需要,还可以传递它

getStuff(passedModel) {
   const { model } = this.state
   const toUseModelInMethod = passedModel || model
   // or 
   const toUseModelInMethod = passedModel ? passedModel : model
}
如果你只是想从这里得到模型,说明它很简单

getStuff() {
   const { model } - this.state
}
还可以将状态传递给方法

getStuff({ model }) {...}
this.getStuff(this.state) // linter is not happy
// that's is better
getStuff(model){...}
const { model } = this.state
this.getStuff(model)

正如我理解您的需要,您希望为来自state的模型提供一个默认var,并且如果需要,还可以传递它

getStuff(passedModel) {
   const { model } = this.state
   const toUseModelInMethod = passedModel || model
   // or 
   const toUseModelInMethod = passedModel ? passedModel : model
}
如果你只是想从这里得到模型,说明它很简单

getStuff() {
   const { model } - this.state
}
还可以将状态传递给方法

getStuff({ model }) {...}
this.getStuff(this.state) // linter is not happy
// that's is better
getStuff(model){...}
const { model } = this.state
this.getStuff(model)

你在那里做的不是解构这与解构无关。这里的错误消息是什么?你在那里做的不是解构这与解构无关。这里的错误消息到底是什么?关于
getStuff({model}=this.state){…
?不。将不会…在参数中仍然存在
this.state,因此它将不被允许。@PatrickRoberts我已经更新了,但不完全确定语法是否会像这样在函数参数中工作。当你说“它将不被允许”你指的是ESLint还是语法?因为在语法上,这是允许的。我指的是ESLint。那么
getStuff({model}=this.state)呢{…
?不。将不会…在参数中仍然有
这个。状态
,因此它将不被允许。@PatrickRoberts我已经更新了,但不完全确定语法是否会像这样在函数参数中工作。当你说“它将不被允许”时“你指的是ESLint还是语法?因为在语法上,这是允许的。我指的是ESLint。”。