I want to do
function MyComponent () {
const myRef = useRef(null)
const myValue = useMemo(() => (myRef.current.someKey), [])
return <div ref={myRef} />
}
But it doesn't work because sometimes myRef is not loaded yet when setting myValue (so myRef.current is null), and I have no idea what should I do to reset myValue when the loading is done.
function MyComponent() {
const [myValue, setMyValue] = useState("Loading...");
const myRef = useRef(null);
useEffect(() => {
setMyValue(myRef?.current.dataset.value);
}, [myRef]);
return (
<div ref={myRef} data-value="foo">
{myValue}
</div>
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.