Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  69] [ 7]  / answers: 1 / hits: 7858  / 3 Years ago, wed, february 10, 2021, 12:00:00

I have two component that one passes some func as prop to another and I am not sure what is the proper way to do this without having to receive an eslint error:


Code:


<Parent>
const doSmthHandler = useCallback((id: number)=> {
//do some stuff
},[])
<ComponentB>
doSmth={()=>doSmthHandler(id)} // Here I get eslint warning: JSX props should not use arrow functions eslint warning
</ComponentB>
</Parent>

Component B receives doSmth prop as function and has a button such as:


<Button onPress={doSmth}>Do stuff</Button>

I wonder how do I pass some argument into the cuntion passed as cb prop into another component that I dont get eslint errors!


More From » reactjs

 Answers
41

The problem with your code is that you're passing a callback as a property directly. You could declare it as a wrapper using useCallback before passing it down to the component, like so:


const doSmthHandlerWrapper = useCallback(
() => doSmthHandler(id),
[id]
)

<ComponentB>
doSmth={doSmthHandlerWrapper}
</ComponentB>

[#1814] Tuesday, February 2, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
karivictoriab

Total Points: 530
Total Questions: 90
Total Answers: 95

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;