how do I use useCallback hook in svelte
Asked Answered
L

2

5

This is my code block looks line when used with a useCallback hook in react but I want to use same function in svelte,but want to add this in useCallback hook. Is there any alternative for svelte.

const newCancelToken = useCallback(() => {
        axiosSource.current = axios.CancelToken.source();
        return axiosSource.current.token;
    }, []);
Lelahleland answered 29/8, 2022 at 7:22 Comment(0)
G
7

Keep that in mind: The way svelte processes its components is very different from react.

React re-renders all components any time any state within a component, or anywhere in a parent, changes. To avoid the re-computation you will need the use of useMemo or useCallback in your case.

Svelte is a compiler and analyzes your template to create targeted DOM update code whenever any concerned state changes. With that in mind, you don't need to memoize such functions with svelte.

Gland answered 29/8, 2022 at 7:41 Comment(0)
B
0

The svelte equivalent of useMemo/useCallback is $:

// react const a = useMemo(() => b + c, [b, c]);

// svelte $: a = b + c;

https://twitter.com/sveltejs/status/1221788690722304003

Bimetallism answered 29/7, 2023 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.