Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document prefilling initialstate #4191

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading