Skip to content

Commit 34c3b5f

Browse files
authored
Merge pull request #4191 from reduxjs/update-entityadapter-docs
document prefilling initialstate
2 parents 2341224 + 581f35c commit 34c3b5f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/api/createEntityAdapter.mdx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,39 @@ const booksSlice = createSlice({
274274
})
275275
```
276276

277+
You can also pass in an array of entities or a `Record<EntityId, T>` object to pre-populate the initial state with some entities:
278+
279+
```js
280+
const booksSlice = createSlice({
281+
name: 'books',
282+
initialState: booksAdapter.getInitialState(
283+
{
284+
loading: 'idle',
285+
},
286+
[
287+
{ id: 'a', title: 'First' },
288+
{ id: 'b', title: 'Second' },
289+
],
290+
),
291+
reducers: {},
292+
})
293+
```
294+
295+
This is equivalent to calling:
296+
297+
```js
298+
const initialState = booksAdapter.getInitialState({
299+
loading: 'idle',
300+
})
301+
302+
const prePopulatedState = booksAdapter.setAll(initialState, [
303+
{ id: 'a', title: 'First' },
304+
{ id: 'b', title: 'Second' },
305+
])
306+
```
307+
308+
The first parameter can be `undefined` if no additional properties are needed.
309+
277310
### Selector Functions
278311

279312
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:

0 commit comments

Comments
 (0)