Questions tagged [usecallback]
251 questions
1
vote
1 answer
Transform function to useCallback
I want to rewrite a function like:
function useQuery() {
return new URLSearchParams(useLocation().search);
}
to preferred by React useCallback form, my best guess was:
const useQuery = useCallback(
() => {
return new…

Tomasz Waszczyk
- 2,680
- 5
- 35
- 73
1
vote
1 answer
Having some troubles getting useEffect, useCallback and setTimeout to work together
Trying to implement some custom scroll behaviour for a slider based on the width. It's using useState, to keep track of the current and total pages inside the slider. This is the code I have ended up with trying to acomplise it but it has some…

meowzart
- 75
- 1
- 8
1
vote
1 answer
Where should I declare the useCallback function?. The function makes the dependencies of useEffect Hook change on every render
I was trying to add the async function in useEffect. While doing this I am getting the warning about wrap fetchData function in useCallback hook. So, Where should I declare the useCallback function, and how to implement it?
Here is the code:
…

pratham82
- 43
- 1
- 6
1
vote
0 answers
React native useCallback change state by useSelector
I take with useSelector the status of the theme type I need, which I find in settings.item.colorScheme.
I have a BottomSheet which allows me that clicking on the selected language to change it, this change changes the status of redux so also of…

Paul
- 3,644
- 9
- 47
- 113
1
vote
0 answers
React Native: Keyboard keeps closing on key press when typing in TextInput
I have a functional component Login Screen and have 4 Input Fields in them, along with a login button.
Here is the Full Code for my component Screen:
export default function Login() {
//Configs
const navigation = useNavigation();
const…

Rishabh More
- 85
- 4
- 11
1
vote
2 answers
Passing data from Child functional component to parent - React/Typescript
Im new in React and typescript.
The thing im trying to do:
I have a class component, which renders a functional camera component.
When the user save an image (from the camera component), i want to use that base64 string in the parent component. Hope…
1
vote
0 answers
React: Optimization callbacks with or without useCallback
TL;DR
Some blogs say that you don't need to use the useCallback hook every time you pass a callback to child component, and sometimes it is better to create a new function every time. Because the cost of useCallback sometimes higher than the actual…

nrofis
- 8,975
- 14
- 58
- 113
1
vote
1 answer
React setState inside useCallback() hook not setting state variable correctly?
I have code that is of the following pattern inside a React FunctionComponent:
const MyComponent: React.FunctionComponent = ({ someArray, someFunction }) => {
const [someStateObjVar, setSomeStateObjVar] = React.useState({});
…

Nodelay Heehoo
- 23
- 1
- 1
- 7
1
vote
1 answer
How to prevent re-rendering with callbacks as props in ReactJS?
I'm practicing with the new hooks functionnality in ReactJS by refactoring the code from this tutorial with TypeScript.
I am passing a callback from a parent component to a child component threw props that has to be executed on a click button…

Thomas Couacault
- 53
- 1
- 4
1
vote
2 answers
How to update a useCallback hook on a event
I need to update a useCallback hook on a certain event that is emitted by eventEmitter3.
Right now I update a local state with the current time to trigger the useCallback. That works but looks crazy complicated. Is there a better solution?
const…

Obiwahn
- 2,677
- 2
- 26
- 36
1
vote
2 answers
React useCallback not updating state onChange on the first change
I'm trying to create a search dropdown list but my search filter is not being updated properly with useCallback. The searchFilter callback does not update values on the first onChange
const [inputValue, setInputValue] = useState('');
const [opts,…

user2456977
- 3,830
- 14
- 48
- 87
1
vote
0 answers
in useCallback is not updated state value
I am try to set a callback in child component, but when call this callback function, state is not updated, that is state in initial state.
Why this is that and how to resolve this problem ?
// calback
const resizeColumn = useCallback(nextWidth => {
…

Tahir Dev
- 11
- 2
1
vote
1 answer
Can you call React's useState and useCallback multiple times in a single functional component? If so, how does it work?
I couldn't find any mention of this yet in the official React docs or on the blagosphere.
I think you can and usually should do something like this when you have more than one state variable:
function MyComponent() {
const [foo, setFoo] =…

dcripplinger
- 125
- 1
- 8
1
vote
1 answer
Why does useCallback with an empty dependency array not return the same function?
I'm trying to write a custom React hook that returns functions which maintain reference equality with every invocation of the hook. Meaning that exactly the same function is returned from the hook every single time, such that comparing them with…

Zach Olivare
- 3,805
- 3
- 32
- 45
1
vote
1 answer
ReactJS: debounce a function that has as an argument a state value; what's the best way to do it?
I already visited this link and tried to follow some examples: Perform debounce in React.js
A bit of context: I'm building a search box that I want to deploy on NPM. Each time the user types, a prop function onSearch is called. This to allow the…

devamat
- 2,293
- 6
- 27
- 50