如何解决“期望在箭头函数中返回值"用reactjs? [英] How to fix "Expected to return a value in arrow function" with reactjs?
问题描述
我的react-app组件有两个功能
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);
})
})
}
运行它,我得到:
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
第26行开始,例如componentDidMount上的Object.keys(this.props.praticiens).map((praticien)=> {
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:64行:预计将在箭头函数array-callback-return中返回一个值
Line 26:64: Expected to return a value in arrow function array-callback-return
由于您不关心从箭头函数返回值,并且不使用 map()
返回的数组,因此应使用 forEach()
函数代替.
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);
})
})
这篇关于如何解决“期望在箭头函数中返回值"用reactjs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!