Comments (26)
The fallback option would be absolutely not fitting here, and regularly go against user expecations.
We have cache collection of data that's not used anymore.
Imagine your hook starts out with initialData
from 3am.
Then you fetch and get data from 5pm.
Then your component unmounts for a minute, the new data gets removed from the cache.
The next moment your component mounts again you are back at data from 3am.
Your data would go backwards in time.
from redux-toolkit.
Like what? The initial state can behave like the request successfully made behind the scenes, so it should be the same as the current behavior.
from redux-toolkit.
Is there any example of how to achieve this with rehydration? The docs on the topic are very scarce, and the API is complicated.
from redux-toolkit.
That was my step 1 above, yes. "Dump your api slice..." ;)
from redux-toolkit.
- On the server, make all requests with RTK Query
In my case, the server is written in Go, is this the only option for using extractRehydrationInfo
?
from redux-toolkit.
Unless you have a custom middleware that is async
, the reducer will always execute synchronously on dispatch and the store will be populated with the required data to fulfill any request.
from redux-toolkit.
@duskpoet
you can give a default value when using this hook, useGetUsersQuery()
const {data = initialState} = useGetUsersQuery()
from redux-toolkit.
But that would unwillingly trigger the api request
from redux-toolkit.
yeah makes sense to have an initial state but might create some confusion in error cases and refetching cases
from redux-toolkit.
You should be able to do this via the hydration process:
https://redux-toolkit.js.org/rtk-query/usage/persistence-and-rehydration
from redux-toolkit.
Yes hydration is fine but useSWR has better way, they take fallback
object, maybe we can take some inspiration from there
https://swr.vercel.app/docs/advanced/understanding.en-US#fallback
from redux-toolkit.
I can try to prepare a PR if this feature does seem reasonable.
from redux-toolkit.
Like I said, I'd rather avoid it - it can be quite confusing. I'd assume that for what you want right now, you could use rehydration.
from redux-toolkit.
@duskpoet Maybe you are looking for something like api.util.updateQueryData
https://redux-toolkit.js.org/rtk-query/api/created-api/api-slice-utils#updatequerydata
from redux-toolkit.
@DaniAcu yea, that's a possibility, though it complicates the usage - need to issue this call and wait for its effects before attaching the store to react, otherwise, there will be an unnecessary API call.
from redux-toolkit.
Is there any example of how to achieve this with rehydration? The docs on the topic are very scarce, and the API is complicated.
Sorry I didn't get around to answer back then.
- On the server, make all requests with RTK Query. Dump your api slice into a script tag or something that will end up in a
window.foo
global in the browser. - In your api:
createApi({
// ...
extractRehydrationInfo(action) {
if (action.type == "rehydrateMe") return action.payload
},
})
- In the browser
store.dispatch({ type: "rehydrateMe", payload: window.foo })
from redux-toolkit.
@duskpoet I found that extractRehydrationInfo
should return the state structure specifically for a createApi slice, maybe the case that @phryneas shared is not enough for your case. I found this work around checking the internals of the library, maybe it could helps you in this case.
import { nanoid } from "@reduxjs/toolkit";
createApi({
// ...
extractRehydrationInfo(action) {
if (action.type === "rehydrateMe") {
return {
queries: {
"getUser()": {
status: 'fulfilled',
endpointName: 'getUser',
requestId: nanoid(),
originalArgs: "",
startedTimeStamp: Date.now(),
data: action.payload,
fulfilledTimeStamp: Date.now()
},
},
mutations: {},
provided: {},
subscriptions: {}
}
}
},
})
Then you could dispatch:
store.dispatch({ type: "rehydrateMe", payload: window.foo })
from redux-toolkit.
@amitdahan
You can also dump an api slice once, look at it's shape and replicate that in go, but you might need to re-do that at one point in the future if we change our spi slice shape.
from redux-toolkit.
This approach still requires to hold off react mounting store to avoid the api request, right?
from redux-toolkit.
How else would you provide "initial data"?
Yes, you have to know the initial data before you mount your React component that needs that initial data.
The example you gave in the initial post would require that, too.
from redux-toolkit.
in our case we have it injected into the page by server renderer, so we can access it like a global variable anytime in the javascript code
from redux-toolkit.
@phryneas Ou I didn't get well then, sorry. I hope the example helps in some way at least.
@duskpoet I think depends on what you mean by "hold off", the dispatch should run before render. But doesn't need to wait for anything.
In my exploration I did:
dispatch({ type: 'Hydrate', payload: window.foo })
const root = createRoot(document.getElementById('root'));
root.render(<App />);
And didn't throw the request because it was already in the store. I would recommend pay attention to parameters of the endpoint but you don't have params in you getUser endpoint.
from redux-toolkit.
Right, in our case, the store is created inside the react tree cause it depends on the routing, so it goes deeper than the react router. And I think the following wouldn't work too:
const store = createStore(...);
store.dispatch({ type: 'Hydrate', payload: window.foo });
return (
<Provider store={store}>
{children}
</Provider>
);
}
because of the thunk middleware
from redux-toolkit.
because of the thunk middleware
What do you mean by that?
from redux-toolkit.
If the chain of reaction to this action will emit an async action, then the react effect may fire first, initiating the API request
from redux-toolkit.
Ok, thanks! Let's close the issue then
from redux-toolkit.
Related Issues (20)
- isSuccess value turning true while status: "pending" HOT 2
- Support for arbitrary properties in RTK Query tags
- Export DraftableEntityState, IsAny, UseQuery and other types HOT 3
- retryCondition() in retry() in version 2.2.4 no longer type checks HOT 2
- feat req: URL building helpers for RTK query? HOT 4
- Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider> HOT 1
- listenerMiddleware/error Error: [Immer] 'current' expects a draft, got: HOT 3
- [request]: Global utility to clear all API cache HOT 3
- Error: Unable to resolve module ./redux-toolkit.production.min.cjs from /Users/alok_prakash/Documents/Projects/buyer-app/node_modules/@reduxjs/toolkit/dist/cjs/index.js: HOT 1
- dispatch does not work, when i try to manually update cache in pessimistically or optimistically. Also show an error in apiSlice.util.updateQueryData("getTodos", undefined, (draft) => {} line show this error "getTodos" "Argument of type 'string' is not assignable to parameter of type 'never'" HOT 4
- Invalidation tags do not work when polling interval is added to query hook HOT 1
- How to connect createListenerMiddleware with RTK Query endpoints call?
- createEntityAdapter add an item on the top on the list instead of the bottom HOT 4
- Selectors don’t have types? HOT 1
- Missing single reducer during INIT HOT 1
- Issue: Circular Dependency in RTK Query with Code-Splitting Endpoints HOT 4
- All createDraftSafeSelector in my app broken, with “No overload matches this call” when running Vite build after migrating from version 2.2.3 HOT 6
- SyntaxError: Unexpected token '??=' when running unit tests with Redux Toolkit 2.2.5 and Jest HOT 1
- RTK query with invalidate tags
- Help migrating from RTK to RTKQ, selecting data from RTKQ
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-toolkit.