Questions tagged [react-usecallback]
57 questions
5
votes
2 answers
why use 'ref' when you use useCallback instead of using useCallback directly
I'm working on React project, and I was investigating some libraries. and I found they used 'useCallback" differently from what I've used.
Below is that code part. I still think this code has no difference from using "useCallback" in a direct way…

김경한
- 57
- 1
- 7
4
votes
2 answers
Is good to use React.useMemo or React.useCallback inside component props?
I was thinking about how to code TailwindCSS cleaner in React. Since Tailwind is utility-first, it makes us inevitably end up with components (ex: className="w-full bg-red-500"). So, I tried to create a utility like this:
utils/tailwind.ts
const tw…

mnrendra
- 162
- 1
- 1
- 8
3
votes
1 answer
ReactJS : useMemo hook fixed my infinite rerender issue instead of useEffect
Without useMemo : infinite rerendering component
I had an issue with the code below compiling successfully but rerendering the component infinite times and I get this error in console : Warning: Maximum update depth exceeded. This can happen when a…

watsum08
- 46
- 5
3
votes
2 answers
Want to understand the dependency in usecallback
const [balance, setBalance] = useState();
const pollBalance = useCallback(async (provider, address) => {
if (provider && address) {
const newBalance = await provider.getBalance(address);
if…

TECH HINDER
- 43
- 4
2
votes
0 answers
Should I use useMemo if my custom hook returns functions created with useCallback?
I'm considering using useMemo in this hook, but I'm not sure if would even provide any benefits.
In the example below, should I return the value with useMemo?
const useBooleanState: IUseBooleanState = (defaultValue) => {
const [value, setValue] =…

Danilo Cunha
- 1,048
- 2
- 13
- 22
2
votes
2 answers
How React.memo works with useCallback
As far as I understand, React.memo is an API that memoize a component: if its props don't change, react use the latest render of that component without comparing it with its previous version. Skipping new render and comparing with old one speed-up…

Nemus
- 1,322
- 2
- 23
- 47
2
votes
2 answers
useRef and useEffect initial access and then later usage of useRef
How can I wait for ref to be ready but also only work inside my use effect with condition?
Here is what I want to do:
I want to select all rows in my DataGrid component using ref when popup is visible.
This code won't work, because I don't have ref…

Figen Güngör
- 12,169
- 14
- 66
- 108
2
votes
1 answer
Should I remove all unecessary usage of usecallback and memo in a React app?
im working on a relatively large React codebase and I've seen that the previous developers used memo and usecallback liberally with the thought that by using these would improve performance. Obviously not, here is an example
export default function…

fardown
- 713
- 2
- 12
- 23
1
vote
5 answers
React useState is not updating when set method is called in an interval
I have a websocket server that sends an object containing some hashes every 15 seconds. When the client receives a hash, I want to check with my current hash. If they differ, I want to make a call to an API to fetch new data.
The socket is working…

BluePrint
- 1,926
- 4
- 28
- 49
1
vote
0 answers
Trying to Fix Dependency warning in React Hook
I built this React Hook to monitor the current window size so that large displayed images can be automatically resized to the space available:
import * as React from 'react';
import type { Rectangle } from 'utils/globalTypes';
/**
* This is a…

RobertW
- 226
- 1
- 2
- 10
1
vote
1 answer
How to document useCallback with jsdoc?
If I have my arrow function, wrapped in a React useCallback, how am I supposed to document it with JSDoc?
const floopPig = useCallback((pigKey, floopCost) => {
const pigAbility = pigs[pigKey];
if (pigAbility.floopCost < magicPoints) {
return…

AncientSwordRage
- 7,086
- 19
- 90
- 173
1
vote
0 answers
how to remove undefined from type to be inferred for useCallback
New useCallback in the react needs to specify args types. I try to specify function type to infer arg type. But if that type contains undefined, it can't infer the correct type.
const onDrop: DropzoneOptions['onDrop'] = useCallback(acceptedFiles…

seyed
- 1,555
- 1
- 17
- 24
1
vote
3 answers
React - how to replace useCallback hook with useMemo?
I am reading this article about memoization in React, and I wonder how can I translate and use useMemo instead of useCallback hook. In particular for this example:
{console.log('Really Skinny Jack')}, []) } />
Where…

Ludwig
- 1,401
- 13
- 62
- 125
1
vote
2 answers
useCallback to fetch data on button click
I have a scenario where I am to fetch data on a button click and store it in a context variable. Please check the code below:
const [products, setProducts] = useState([]);
const handleButtonClick= useCallback(() => {
if (countryCode) {
…

bazinga
- 87
- 2
- 9
1
vote
0 answers
React custom hook with callback parameter not picking up parameter change
I have a following generic custom hook. What I want to achieve is that hook itself exposes api with api functions, which I can use in a callback. I also want a hook to be dependent on a api function parameters changes.
export const useArticleApi =…

M.Pogorzelski
- 69
- 5