Skip to content

Instantly share code, notes, and snippets.

@marekpiechut
Created June 22, 2021 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marekpiechut/2f0605d529670a3f7a652c55e05b68b9 to your computer and use it in GitHub Desktop.
Save marekpiechut/2f0605d529670a3f7a652c55e05b68b9 to your computer and use it in GitHub Desktop.
React-profiler
import {
unstable_trace as trace,
unstable_wrap as wrap,
} from 'scheduler/tracing'
const useData = () => {
const [loading, setLoading] = useState(false)
const [data, setData] = useState(null)
const fetch = () => {
setLoading(true)
fetchData()
.then(
wrap(data => {
setData(data)
})
)
.finally(wrap(() => setLoading(false)))
}
return { fetch, loading, data }
}
const Component = () => {
const { fetch, loading, data } = useData()
return <button
onClick={() => trace('Fetch data', performance.now(), fetch)}
>
Fetch data
</button>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment