Skip to content

Removal of onSuccess causes a lot of useEffects #6451

Answered by TkDodo
oren-legit asked this question in Q&A
Discussion options

You must be logged in to vote

I have a blogpost on this topic: https://tkdodo.eu/blog/react-query-and-forms

I would prefer the "derived state" solution still:

const [userSelection, setSelectedOptionIds] = useState(undefined)

const { data: defaultSelectedIds } = useQuery

const selection = userSelection ?? defaultSelectedIds ?? []

It's the most simple code. Here, we start the userSelection with undefined, because the user hasn't selected anything. Then, we fall back to server state when that's undefined. That means when the query is loaded, the user will see the defaultSelectedIds as selected. As soon as they change something, userSelection will be different than undefined, so they will always see the user selection.…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@oren-legit
Comment options

@TkDodo
Comment options

Answer selected by oren-legit
Comment options

You must be logged in to vote
1 reply
@TkDodo
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants