Created
February 20, 2026 06:04
-
-
Save suhaotian/8fae6308a96a1851bd018430e75a3bce to your computer and use it in GitHub Desktop.
nextTick for react, idea from vue.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use client"; | |
| import { useState, useRef, useLayoutEffect } from "react"; | |
| import useNextTick, { useNextTickLayout } from "use-next-tick"; | |
| function MyComponent() { | |
| const [count, setCount] = useState(0); | |
| const ref = useRef<HTMLSpanElement>(null); | |
| const nextTick = useNextTick(); | |
| /* | |
| const nextTick = useNextTickLayout(); // if you need `useLayoutEffect` instead off `useEffect` | |
| */ | |
| const handleClick = () => { | |
| setCount((c) => c + 1); | |
| nextTick(() => { | |
| console.log(ref.current?.textContent); // "1" ✓ | |
| }); | |
| }; | |
| return <span ref={ref}>{count}</span>; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment