Skip to content

Commit

Permalink
Merge pull request #4191 from reduxjs/update-entityadapter-docs
Browse files Browse the repository at this point in the history
document prefilling initialstate
  • Loading branch information
EskiMojo14 committed Feb 12, 2024
2 parents 2341224 + 581f35c commit 34c3b5f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/api/createEntityAdapter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,39 @@ const booksSlice = createSlice({
})
```

You can also pass in an array of entities or a `Record<EntityId, T>` object to pre-populate the initial state with some entities:

```js
const booksSlice = createSlice({
name: 'books',
initialState: booksAdapter.getInitialState(
{
loading: 'idle',
},
[
{ id: 'a', title: 'First' },
{ id: 'b', title: 'Second' },
],
),
reducers: {},
})
```

This is equivalent to calling:

```js
const initialState = booksAdapter.getInitialState({
loading: 'idle',
})

const prePopulatedState = booksAdapter.setAll(initialState, [
{ id: 'a', title: 'First' },
{ id: 'b', title: 'Second' },
])
```

The first parameter can be `undefined` if no additional properties are needed.

### Selector Functions

The entity adapter will contain a `getSelectors()` function that returns a set of selectors that know how to read the contents of an entity state object:
Expand Down

0 comments on commit 34c3b5f

Please sign in to comment.