Questions tagged [usecallback]

251 questions
0
votes
1 answer

React Hook useCallback has a missing dependency or Maximum update depth exceeded

In a useEffect , I check if an object exists, if the object exists I call a useCallBack function which will fill in the values of a form via formik Here is the problem : 1- if I do not fill in the formDef in the dependencies of useCallBack, I get a…
Greg-A
  • 772
  • 1
  • 19
  • 41
0
votes
1 answer

useRef passed in as parameter needs to be added to dependencies?

From the following two questions, I understand that useRef does not need to add update dependencies. Related questions I looked at: https://stackoverflow.com/a/60476525/9854149 https://stackoverflow.com/a/63353582/9854149 But when useRef is used…
weiya ou
  • 2,730
  • 1
  • 16
  • 24
0
votes
2 answers

Generator function inside useCallback is returning same values in react, How to solve this?

I am creating to-do app in react and for the id of task i am using generator function. But This generator function is giving value 0 everytime and not incrementing the value.I think the reason for issue is useCallback() hook but i am not sure what…
front_dev_j
  • 139
  • 1
  • 12
0
votes
1 answer

useCallback dependency array defeats the purpose of preventing re-renders when updating an array

The following React app displays a list of fruits. Each Fruit has a "add to fav" button which uses a addFav callback to add said fruit to a list of favourites in the parent. Passing in the handleAddFav callback causes unnecessary re-renders, so I…
Embedded_Mugs
  • 2,132
  • 3
  • 21
  • 33
0
votes
2 answers

React.Js - useCallback inside a callback generator

I have a bunch of checkbox components and I want to update a state of the parent component each time the checkbox components are toggled. Right now I'm doing this: const selChangeHandler = useCallback( (checkboxId, isSel) => { if…
0
votes
1 answer

React - reset state to empty array if more than 2 items selected

I'd like to reset my selectedDogs state to 0 if more than 2 dogs are selected. How can I do this? At the moment, the array just keeps growing past 2. If 2 dogs are selected, I'd like to trigger an action (e.g. write to console or something). const…
methuselah
  • 12,766
  • 47
  • 165
  • 315
0
votes
1 answer

onLongPress is not working same as onClick

I am passing onLongPress on a Component that has the same function as onClick. I have used useState for both. When I long press the item it opens the menu but again if I Long press the menu stays open. const resetNavItems = () => { …
Curious_guy
  • 161
  • 11
0
votes
0 answers

Can you use useCallback without assigning it to a variable?

// Inside a functional component. return ( { // Do something... }, [a, b])} /> ); I'm wondering if the code above works the same as below. Will it still memoize the function as…
han
  • 97
  • 1
  • 1
  • 5
0
votes
1 answer

Understanding Exhaustive deps ESlint warning with n useEffect

Cant seem to get my head around the right approach for this. Want to get ride of the warning and not by having the inline ignore warning comment. Probably useCallback is in the direction of the solution. I just want to check only once when…
San Jay Falcon
  • 993
  • 1
  • 9
  • 20
0
votes
1 answer

useCallback returns n+1 times where n is the number of state variables

I'm trying to set up a custom context menu, however whenever the user right clicks the context menu function returns 6 separate times, the 5th being what I need and the 6th being the default state values. However if the user double right-clicks in…
0
votes
1 answer

React useEffect dependency or useCallback seem to cause re-renders

I have a useEffect function that takes an image, resizes it for display and makes it ready for upload. While this works I get the React warning to list the function in the useEffect dependency list. But when I do this it causes continuous re-renders…
ngwp
  • 47
  • 1
  • 7
0
votes
0 answers

useCallback - prevent infinite loop

I'm having a function object wrapped inside an useCallback statement which needs to invoke a function that is being injected via props: const update = useCallback(() => { /*some stuff*/ props.OnUpdate(); }, [..other_args,…
bob_saginowski
  • 1,429
  • 2
  • 20
  • 35
0
votes
1 answer

Is decoupling callbacks using refs safe?

This hook takes a function that created by using useCallback (which will update every time its dependencies do) and returns a function that calls the updated callback while not updating itself. While in some cases you might care when a callback is…
BanForFun
  • 752
  • 4
  • 15
0
votes
3 answers

React canceling clearTimeout not working properly on useEffect

https://codepen.io/evan-jin/pen/qBjGWvR const [hoverItem, setHoverItem] = useState(null) const timerRef = useRef(null) const addcursor = useCallback(() => { console.log('addcursor') clearTimeout(timerRef.current) …
Evan Jin
  • 123
  • 6
0
votes
1 answer

FlatList renders each time view loads, even after using useCallback

I have an array of objects which is displayed in a FlatList. And I have TextInputs and a Button. On button click values from TextInputs are added to the existing FlatList. It works fine. But the FlatList is rendered many times even when typing in…
Arun P S
  • 11
  • 2