Skip to content

Instantly share code, notes, and snippets.

@suhaotian
Created February 20, 2026 06:04
Show Gist options
  • Select an option

  • Save suhaotian/8fae6308a96a1851bd018430e75a3bce to your computer and use it in GitHub Desktop.

Select an option

Save suhaotian/8fae6308a96a1851bd018430e75a3bce to your computer and use it in GitHub Desktop.
nextTick for react, idea from vue.js
"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