javascriptreactjsobjecteslintarrow-functions

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


I have two function in my react-app components

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

How can I fix it ?


Solution

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

    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.

    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);
                })
            })