Best practice for derived local states updates #6220
Replies: 2 comments 2 replies
-
I pretty much explain the possibilities in my blog: |
Beta Was this translation helpful? Give feedback.
-
I saw that blog, but I didn't feel it was answering the question because of how tied the solution is with react-hook-form. Translating it to our case: The first proposed approach would require to keep track of 4 references in my example and would not allow to reflect data changes in the server as only the initial value is set in the inputs (unless a useEffect is again used to update the value of those refs). The second one looks like what we want, but I think we would need to switch to interface Contact {
id: number;
name?: string;
email?: string;
telephone?: string;
homeAddress?: string;
}
function ContactEditor({
contact,
onSubmit,
}: {
contact: Contact;
onSubmit: (contact: Contact) => void;
}) {
const [localContact, setLocalContact] = useState<Contact>({
id: contact.id,
name: undefined,
email: undefined,
telephone: undefined,
homeAddress: undefined,
});
return (
<div>
<input
value={localContact.name ?? contact.name}
onChange={(e) =>
setLocalContact({ ...localContact, name: e.target.value })
}
/>
<input
value={localContact.email ?? contact.email}
onChange={(e) =>
setLocalContact({ ...localContact, email: e.target.value })
}
/>
<input
value={localContact.telephone ?? contact.telephone}
onChange={(e) =>
setLocalContact({ ...localContact, telephone: e.target.value })
}
/>
<input
value={localContact.homeAddress ?? contact.homeAddress}
onChange={(e) =>
setLocalContact({ ...localContact, homeAddress: e.target.value })
}
/>
<button
onClick={() => {
onSubmit(localContact);
setLocalContact({
id: contact.id,
name: undefined,
email: undefined,
telephone: undefined,
homeAddress: undefined,
});
}}
>
Update Contact
</button>
</div>
);
} but this doesn't : interface Contact {
id: number;
name: string;
email: string;
telephone: string;
homeAddress: string;
} Anyway, I think this answer my question, maybe we should just move to react-hook-form for these cases. Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
my company uses react-query extensively to sync and update server states in our react frontend. Recently, we have seen a pattern emerging and I'm not sure whether we are following the best practice. I've searched for related discussion but either my keywords are not the correct ones or we are doing something quite wrong, hence this question.
What is the recommended way to keep a local state which is derived from a
query
result, so that:?
I'm aware of the fact that
react-query
is not a client state manager, but surely this is a common enough issue to have an advocated approach.Here is an example of what we are doing right now:
So that we have:
I'm not a huge fan of
useEffect
in general and I've tried to minimise its usages in our codebase, so this approach smells a bit to me.Few other ideas that come to my mind:
Do you reckon this is how it should be done, or am I not seeing some other better approach here?
Beta Was this translation helpful? Give feedback.
All reactions