简体   繁体   English

如何使用 reactjs 修复“预期在箭头函数中返回值”?

[英]How to fix "Expected to return a value in arrow function" with reactjs?

I have two function in my react-app components我的 react-app 组件中有两个 function

componentDidMount() {
        if(Object.keys(this.props.praticiens).length>0)
            Object.keys(this.props.praticiens).map((praticien) => {
                if(this.props.praticiens[praticien].identifiant_user === getUrlParams(window.location.hash).identifiant_user)
                    this.setState({
                        praticien:this.props.praticiens[praticien].id_user
                    })
            })
    }

    handleChangePraticien = (praticien) => {
        this.setState({ praticien }, () => {
            Object.keys(this.props.praticiens).map((praticien) => {
                if(this.state.praticien === this.props.praticiens[praticien].id_user)
                    this.props.selectPraticienFunction(this.props.praticiens[praticien].identifiant_user);
            })
        })
    }

When I run it, I get:当我运行它时,我得到:

Line 26:64:  Expected to return a value in arrow function  array-callback-return
  Line 36:64:  Expected to return a value in arrow function  array-callback-return

Such as the Line 26 is beginning Object.keys(this.props.praticiens).map((praticien) => { on componentDidMount and Line 36 is the same line on the handleChangePraticien function比如第 26 行开始Object.keys(this.props.praticiens).map((praticien) => {在 componentDidMount 和第 36 行是同一行上的 handleChangePraticien function

How can I fix it?我该如何解决?

Line 26:64: Expected to return a value in arrow function array-callback-return第 26:64 行:预期返回箭头 function 数组回调返回中的值

Since you do not care about returning a value from the arrow function and you are not using the array returned by the map() , you should be using forEach() function instead.由于您不关心从箭头 function 返回值并且您没有使用map()返回的数组,因此您应该使用forEach() function 代替。

componentDidMount() {
        if(Object.keys(this.props.praticiens).length>0)
            Object.keys(this.props.praticiens).forEach((praticien) => {
                if(this.props.praticiens[praticien].identifiant_user === getUrlParams(window.location.hash).identifiant_user)
                    this.setState({
                        praticien:this.props.praticiens[praticien].id_user
                    })
            })
    }

    handleChangePraticien = (praticien) => {
        this.setState({ praticien }, () => {
            Object.keys(this.props.praticiens).forEach((praticien) => {
                if(this.state.praticien === this.props.praticiens[praticien].id_user)
                    this.props.selectPraticienFunction(this.props.praticiens[praticien].identifiant_user);
            })
        })

Changing {} to () worked for me将 {} 更改为 () 对我有用

from map(() => {}) to map(() => ())map(() => {})map(() => ())

The Array.prototype.forEach() and Array.prototype.map() are used for two differents purposes. Array.prototype.forEach() 和 Array.prototype.map() 用于两个不同的目的。

  • Array.prototype.forEach() Array.prototype.forEach()

The Array.prototype.forEach is used to simply loop over an array's items but doesn't return any special value. Array.prototype.forEach 用于简单地遍历数组的项,但不返回任何特殊值。 Its signature is:它的签名是:

forEach(callback, [thisArg]) => undefined

Only the first argument callback is mandatory and its signature is (current [, index, array]) => void where the only required argument is current只有第一个参数callback是强制性的,它的签名是(current [, index, array]) => void其中唯一需要的参数是current

Example例子

[1,2,3].forEach( item => console.log(item))
// Output 1 2 3 undefined
  • Array.prototype.map() Array.prototype.map()

This function is mostly used to create a new Array from an other one.这个 function 主要用于从另一个数组创建一个新Array Unlike the forEach function, it returns an Array .forEach function 不同,它返回一个Array Its signature is the following:它的签名如下:

forEach(callback [, thisArg]) => Array . forEach(callback [, thisArg]) => Array Just like forEach , only the first argument is mandatory but the callback signture is different: (current [, index, array]) => any .就像forEach一样,只有第一个参数是强制性的,但回调签名不同: (current [, index, array]) => any The final array will contain every value returned by the callback after each iteration.最终数组将包含每次迭代后回调返回的每个值。

Example例子

[1,2,3].map(item => item ** 2)
// Output [1 4 9]

More informations over there:那里的更多信息:

Array.prototype.forEach() Array.prototype.forEach()

Array.prototype.forEach() Array.prototype.forEach()

You're using map as though it were forEach .您正在使用map ,就好像它是forEach一样。 Don't use map unless you're going to use the array it creates from the return values of the callback.不要使用map除非您要使用它从回调的返回值创建的数组。 Use forEach , for-of , or any of the many other ways to loop through arrays described in this answer .使用forEachfor-of或任何其他方法循环通过此答案中描述的 arrays 。

this is just one of Errors: Array.prototype.map() expects a return value from arrow function.eslintarray-callback-return这只是错误之一:Array.prototype.map() 期望箭头 function.eslintarray-callback-return 的返回值

{cableCars.map((cableCar: typeCblCar) => {
  <CableCar key={cableCar.id} cableCar={cableCar} dispatch={dispatch} />
})}

Does not work, but this implicite return works perfect:不起作用,但是这个隐式返回非常完美:

{cableCars.map((cableCar: typeCblCar) => (
  <CableCar key={cableCar.id} cableCar={cableCar} dispatch={dispatch} />
))}

This was my problem with useReducer in TypeScript.这是我在 TypeScript 中的 useReducer 的问题。 The answer gived by heremyas solve my agony So from my over-complicated workaround this is pure beauty. heremyas 给出的答案解决了我的痛苦所以从我过于复杂的解决方法来看,这是纯粹的美。 Thanks heremyas and long live stackoverflow!感谢 heremyas 和万岁的 stackoverflow!

Problem: Array.prototype.map() expects a return value from arrow function array-callback-return问题: Array.prototype.map() 期望箭头 function array-callback-return 的返回值

Solution: You can find Solution Here: https://youtu.be/o6GYl3nlIcI解决方案:您可以在此处找到解决方案: https://youtu.be/o6GYl3nlIcI

If you need to return an array, but also place conditionals in the loop so that some items may not be returned you can combine .forEach with .push like this:如果您需要返回一个数组,但还要在循环中放置条件,以便可能不返回某些项目,您可以将.forEach.push组合起来,如下所示:

class Profiles extends Component {
  profilesArray() {
    let profiles_array = [];

    this.props.profiles.forEach((profile) => {
      // some condition
      if (profile.id !== this.props.whatever) {
        profiles_array.push(
          <ContentCard key={profile.profile_id}>
            <p>My Content</p>
          </ContentCard>
        );
      }
    });

    return profiles_array;
  }
  render() {
    return (
      <>
        <h1>Profiles</h1>
        {this.profilesArray()}
      </>
    );
  }
}

you need to return something for each iteration when you iterate a map, so i suggest to make these alterations.当您迭代 map 时,您需要为每次迭代返回一些东西,所以我建议进行这些更改。

list.map(() => {
   return (
    // block of code to be executed
   )
})

or simply或者干脆

list.map(() => (
    // block of code to be executed
))

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

相关问题 预期在 reactjs 上的箭头函数末尾返回一个值如何修复此错误? - Expected to return a value at the end of arrow function on reactjs how to fix this error? 如何解决此“预期在箭头函数中返回值”错误 - How to fix this "expected to return a value in arrow function" error 如何修复“预期在箭头函数数组回调返回中返回一个值”? - How to fix "Expected to return a value in arrow function array-callback-return"? 如何修复警告“预计在箭头 function 数组回调返回中返回一个值” - How fix warning “Expected to return a value in arrow function array-callback-return” 如何修复预期在反应js中的箭头function警告末尾返回值? - How to fix expected to return a value at the end of arrow function warning in react js? 如何修复“预期在箭头函数末尾返回一个值”警告? - How do I fix "Expected to return a value at the end of arrow function" warning? 箭头 function 预期返回值 - Arrow function expected a return value 如何解决 eslint 中的“预期在箭头函数中返回值”错误 - How to solve "Expected to return a value in arrow function" error in eslint 预期在react中的箭头函数末尾返回一个值 - Expected to return a value at the end of arrow function in react 重构:期望在箭头函数的末尾返回一个值 - Refactor: Expected to return a value at the end of arrow function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM