useChartReload
Drives the reload button of a chart card: a loading skeleton while your task runs, then the chart draws in again.
import { useChartReload } from "@wunderui/react/charts"Playground
Installation
useChartReload comes with WunderUI Core and Pro, in the entry of the package from your plan. See the plans. Then import it:
import { useChartReload } from "@wunderui/react/charts"When to use
Use useChartReload for
- A chart card with a Reload button: starts it, shows the skeleton.
- Refetching the data of a chart and drawing the chart in again afterwards, also when the new data equals the old.
- A loading state that is readable: the skeleton stays for at least two steps (900 ms by default), however fast the task is.
Use something else for
- useChartAnimation for the draw-in of a chart you build with Recharts yourself. It does not reload anything.
- useLoadSequence for a whole dashboard page that opens with skeletons and a build-up. One chart does not need it.
- A data library for fetching, caching and retrying. Pass its refetch function as the task and keep the loading state of this hook for the chart.
Signature
switches to , runs the task you pass (fetching new data, for example) and waits at least two steps, so the skeleton is readable rather than a flash. Then returns to and goes up; pass it as so the chart plays its enter sequence again, also when the data came back unchanged.
useChartReload(): ChartReloadReturns
An object with the state to hand to the chart and its card, a counter for the enter sequence and the function that runs a reload. The object is new on every render; itself keeps its identity.
Behavior and motion
The hook holds two pieces of state and a timer. The skeleton and the draw-in belong to the chart.
- State
- is and is at the start. sets , starts your task and a timer of two (read from the stylesheet on every call, 450 ms each when it cannot be read) side by side, and waits for both. A fast task therefore never shows the skeleton for less than the timer, a slower task decides how long it stays. Then is and goes up by one.
- Errors
- If the task throws or rejects, the wait ends at once, without waiting for the timer. goes back to , still goes up, and rejects with the error. Catch it where you call ; otherwise it is an unhandled rejection. The chart keeps the data it had and draws in again.
- Overlapping calls
- The hook neither queues nor cancels a second while one runs: the first one to finish sets . disables its Reload button while is ; do the same with a button of your own.
- Server rendering
- Rendering uses only state, no browser API, so server and client render the same. The stylesheet is read inside , which you call from an event handler in the browser.
- Cleanup
- Nothing is cleaned up. The timer is not cancelled when the component unmounts; setting state afterwards has no effect.
- Motion
- The hook animates nothing itself. The chart shows its chart-shaped skeleton while is and plays its enter sequence when changes. New replays that sequence by itself.
- Reduced motion
- The loading state and the minimum wait stay. With reduced motion the chart comes back without the draw-in.
Best practices
- Do
Pass to the chart and to . If one of them misses it, the card shows its content while the chart shows a skeleton.
- Do
Pass as . Without it a reload that returns the same data shows the skeleton and then the finished chart, without the draw-in.
- Do
Set the new data inside the task. The skeleton then ends with the new data on screen, and the draw-in plays on it.
- Do
Catch the error of and tell people the refresh failed. The hook ends the loading state; it does not show a message.
- Don’t
Do not call to refresh data in the background or on a poll. Every call shows the skeleton for at least 900 ms. Update the data and let the chart replay by itself.
- Don’t
Do not put the returned object in an effect's dependency list. It is new on every render. Use , which is stable.
- Don’t
Do not use for a failed request. It only knows and ; keep an error message of your own, or pass to the chart yourself.
Notes for AI
Give your coding assistant the page or the MCP call instead of screenshots.
get_component("useChartReload")import { useChartReload } from "@wunderui/react/charts"The full library index for assistants is at /llms.txt.