reduxjs/redux-toolkit

RTK Optimistic Updates in "Select" element default value not working without refreshing page

rosgeorg opened this issue · 3 comments

Discussed in #4325

Originally posted by rosgeorg April 4, 2024
Hello, I'm facing this situation where I'm trying to modify the default value of a select (dropdown) element using optimistic updates, but even when my query's cache is correctly updated after mutation, the default value of this select won't change until I refresh the page. Is there a way to fix this?

I also added a "div" with the same data inside of it, and it get's updated correctly inside of the div, but not in the default value

**** So, this was marked as resolved but maybe I did not explain myself correctly, and I think I was missing some important details:

Even if I pick value or defaultValue it is the same result. The thing is I have 2 select/dropdown elements in different parts of the UI which control and display the same data. One of them is inside a modal with a "Save" button which triggers a mutation, which optimistically updates the cache from the query with the selected value.

What I'm trying to do is to make the other select/dropdown to display the data submitted by the first select as soon as the mutation gets triggered, and of course, that modified value should become the new "defaultValue" for that 2nd select element****

Screenshot 2024-04-04 at 7 44 15 p m

Thanks in advance for your help

FWIW, I did already answer your question over in the other thread :)

Hello @markerikson, I did look at the answer, but again, maybe I'm not explaining myself clearly? I think that answer is for a different question. In this case, I'm looking to have a "defaultValue" because that's the value that I want the user to see every time it opens that modal. So "value" is not what I'm trying to update but the initial (or. default) value

That's my point, though. defaultValue only sets the initial value of an input in React. It does not ever update the value at all.

Also, this doesn't actually have anything to do with Redux or RTKQ - that's really a question about React .