How to pass $derived or $state variable into a function parameter... #13080
-
Hello, I'm new to Svelte 5 (also, almost newbie with Svelte 4), and I have a question.
export const useAPIState = (apiParams: callAPIParams) => {
const response: ApiResponse = $state({ data: null, loading: false, error: null })
let { data, loading, error } = response
const loadData = async () => {
// ...
try {
.. ///
const res = await callAPI(apiParams)
.. ///
} catch (e) {
..///
}
}
return { data, loading, error, loadData }
} I want to make apiParams in useAPIState "ALWAYS CURRENT" (reactive)... But I don't know how to do. Also, ESLint: State referenced in its own scope will never update. Did you mean to reference it inside a closure?(state_referenced_locally)(svelte/ valid-compile) appeared. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There are a few ways to keep reactivity while passing a value somewhere:
useAPIState({
key: 'login',
get data() { return params; },
}); and reading |
Beta Was this translation helpful? Give feedback.
There are a few ways to keep reactivity while passing a value somewhere:
() => someValue
and then call it to get a fresh reactive value;and reading
apiParams.data
will always return a fresh data.