-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update App.svelte by destructuring of an Object #258
base: main
Are you sure you want to change the base?
Conversation
Destructured the response object to have a better comparison with other frameworks / libraries as the others also does the same destructuring.
@matschik @pablo-abc Would you mind to review the changes please? |
In Svelte 5 destructuring this is not possible since it would lose reactivity. The value of the property is computed on destructure rather than on access. |
Yeah, I just test the code on Svelte 5 preview and your right, it loses the reactive nature if you destructuring the state object. Hence closing the PR. |
Used $derived state to get the reactive values.
Used the $derived state to get the reactive values and it's working now. Would you mind to review the changes please? @pablo-abc |
I haven't dug that much into how runes work but I'm quite surprised that works :p seems to be expected behaviour, though? I see some pitfalls mentioned with doing it this way, though. There's two solutions at the end of that issue that seem slightly better? E.g. without modifying const response = useFetchUsers();
const { isLoading, errors, users } = $derived(response); Although I see no harm in this specific scenario... may be ok already. I'm not sure what you think, @matschik. Final decision is yours in the end 😄 On an unrelated note, I know it has nothing to do with your PR but I don't see why the example is using a export default function useFetchUsers() {
let users = $state();
let error = $state();
let isLoading = $state(false);
async function fetchData() {
isLoading = true;
try {
const response = await fetch("https://randomuser.me/api/?results=3");
users = (await response.json()).results;
error = undefined;
} catch (err) {
error = err;
users = undefined;
}
isLoading = false;
}
fetchData();
return {
get users() { return users },
get error() { return error },
get isLoading() { return isLoading },
};
} |
Updated the code with suggested review changes
Removed extra spaces.
Destructured the response object to have a better comparison with other frameworks / libraries as the others also does the same destructuring.