Redux-toolkit: Could configureStore preloadedState use Partial instead of DeepPartial ?

Created on 7 Jan 2021  路  3Comments  路  Source: reduxjs/redux-toolkit

Code Sandbox link with this example

It's got a state shaped like this:

{
  SLICE_A: {
    foo: string,
    bar: string
  },
  SLICE_B: {
    foo: string,
    bar: string
}

I'm implementing SSR for my web app. So everything that's async on client, will have to be preloaded on my server for SSR. And I'll need to use configureStore and add those preloaded properties.

Let's say I need to preload SLICE_A.foo:

The first thing I saw was:

image

Then I thought: "Great! configureStore already knows that I might not need to preload my entire state object, so it made all my slices optional with Partial<MY_ROOT_STATE>".

Then I continued and I got:

image

And I thought: "Awesome! It even knows that I might not need to preload my full SLICE_A, so it made everything optional. I'll just override SLICE_A.foo then".

So I did:

image

And I logged the result on App.ts:

image

And for my surprise, this was the result:

image

The property SLICE_A.bar is now gone.

I was expecting the preload of SLICE_A.foo to be done with something like:

SLICE_A = {
  ...INITIAL_STATE_SLICE_A,           // THE ONE FROM createSlice
  ...PRELOADED_STATE_SLICE_A
}

So SLICE_A.bar would be untouched.

What I mean to ask with this issue is: if configureStore is going to replace the entire slice with the "preloaded slice", shouldn't it make the slices as optional, but the "inner parts" of the slices to still be required (if they are required)?

Something like:

prealodedState: {
  SLICE_A?: {
    foo: string,
    bar: string
  },
  SLICE_B?: {
    foo: string,
    bar: string
  }
} 

Instead of making foo and bar optional as well.

From the docs, it seems that it uses DeepPartial. Why is that? Why doesn't it use just Partial?

https://redux-toolkit.js.org/api/configureStore

image

Most helpful comment

Yeah, it's a type error. Correct would be a normal Partial<RootState>, not DeepPartial. We'll fix that in the next RTK release.

All 3 comments

I'm getting the same behavior
the preloaded state would just completely overwrite my default state and remove it

Yeah, it's a type error. Correct would be a normal Partial<RootState>, not DeepPartial. We'll fix that in the next RTK release.

Should be fixed by #949 .

Was this page helpful?
0 / 5 - 0 ratings