Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I trigger SvelteKit to rerender a specific field

I have a document object containing fields like title and name.

When an operation fails, I’m updating the document special field _failures with the error messages, for example I update

doc._failures.title = {failCode: 404, error: 'document unavailable'}

Now in sveltekit, when I display the fields, I check if there’s not a corresponding error in doc._failures.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My problem is that when I update _failures, Sveltekit doesn’t ‘realize’ I added fields into it, and never renders the failure messages.

I have a component tree I’m providing the root of with doc, but I’d like only nested fields to refresh if there’s a failure associated to them. For example doc.contact.email.personal

Any suggestions?

NOTE:
Just if you wonder how my code adds the _failure here’s a snippet:

if (result.type === 'failure') {
    const error = JSON.parse(result.data).error;
    console.log("Adding _failure to ", accessor)
    _set(doc, '_failures.'+accessor, { failCode:result.status, error } )
}

>Solution :

Reactivity is based on assignments, _set is not an assignment.

You could add doc = doc afterwards to invalidate doc. This will cause any UI to update that uses doc reactively (i.e. if the reactivity chain is not broken that should be enough).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading