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

Cod*_*ver 5 javascript object reactjs eslint arrow-functions

我的 react-app 组件中有两个函数

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);
            })
        })
    }
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到:

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
Run Code Online (Sandbox Code Playgroud)

例如第 26Object.keys(this.props.praticiens).map((praticien) => {行在 componentDidMount上开始,第 36 行在 handleChangePraticien 函数上是同一行

我该如何解决?

小智 23

将 {} 更改为 () 对我有用

map(() => {})map(() => ())

  • 这对我有用。你能解释一下其中的区别吗? (2认同)

Rah*_*obe 21

第 26:64 行:预期在箭头函数 array-callback-return 中返回一个值

由于您不关心从箭头函数返回一个值,并且您没有使用由 返回的数组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);
            })
        })

Run Code Online (Sandbox Code Playgroud)


小智 7

Array.prototype.forEach() 和 Array.prototype.map() 用于两个不同的目的。

  • Array.prototype.forEach()

Array.prototype.forEach 用于简单地循环数组的项,但不返回任何特殊值。它的签名是:

forEach(callback, [thisArg]) => undefined

只有第一个参数callback是强制性的,它的签名是(current [, index, array]) => void唯一需要的参数所在的地方current

例子

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

此函数主要用于Array从另一个函数创建一个新函数。与forEach函数不同,它返回一个Array. 它的签名如下:

forEach(callback [, thisArg]) => Array. 就像forEach,只有第一个参数是必需的,但回调签名不同:(current [, index, array]) => any。最终数组将包含每次迭代后回调返回的每个值。

例子

[1,2,3].map(item => item ** 2)
// Output [1 4 9]
Run Code Online (Sandbox Code Playgroud)

更多信息在那里:

Array.prototype.forEach()

Array.prototype.forEach()


T.J*_*der 6

你正在使用map,好像它是forEach. map除非您打算使用它从回调的返回值创建的数组,否则不要使用。使用forEachfor-of或许多其他方法中的任何一种来循环遍历此答案中描述的数组。