I’m trying to set the value of input using ref but I’m getting ref null inside useCallback Hook.
let inputRef = useRef();
const search = useCallback(
(data) => {
console.log(inputRef);
},
[inputRef],
);
return <input type="text" ref={inputRef} />
and It’s showing null
in the browser console when I call this function.
this is just an example showing, what I’m trying to achieve.
Probably you need to pass
null
touseRef()
first. Then you can access withinputRef.current
.Try as the following:
From the documentation of
useRef()
:I hope this helps!
The problem isn’t that he’s not passing in a null value, a ref is initialized to null by default.
The problem is that useCallback memoizes and caches the result of this callback function before the first render, before the component to which this ref is assigned has been mounted. It then waits for inputRef to change but inputRef is just a reference to an object and even if the component the ref points to changes, the value of inputRef does not as it is only being compared by reference equality
So, since the function has no reason to update as the value of inputRef is not going to change — it evaluates to the cached result of the search callback as it was before the DOM mounted and outputs null to console.
Hvomuk – viagra tadalafil generique Lapkqq dynsqh
Zvdxqj – help with corporate finance homework Ykccxi zqsceh
Lndqqg – writing an application essay Quctuw spsoef
Ucujfl – essay writing website reviews Mjqeaj tmzedp
Vehlfu – silagra 50 mg india Iawxbv laxoxy