@rbxts/pretty-react-hooks
Version:
Useful hooks for @rbxts/react
41 lines (27 loc) • 796 B
Markdown
## 🪝 `useLatestCallback`
```ts
function useLatestCallback<T extends Callback>(callback: T): T;
```
Returns a memoized callback that always points to the latest version of the callback.
When passed a new callback, the return value will not change, but calling it will invoke the new callback.
### 📕 Parameters
- `callback` - The callback to memoize.
### 📗 Returns
- The memoized callback.
### 📘 Example
```tsx
interface Props {
onStep: () => void;
}
function Stepper({ onStep }: Props) {
const onStepCallback = useLatestCallback(onStep);
useEffect(() => {
// Will always call the latest version of `onStep`
const connection = RunService.RenderStepped.Connect(onStepCallback);
return () => {
connection.Disconnect();
};
}, []);
return undefined!;
}
```