From 581f35cabbc8562558a8f0ef58e7b54eed26a599 Mon Sep 17 00:00:00 2001 From: EskiMojo14 Date: Mon, 12 Feb 2024 22:46:54 +0000 Subject: [PATCH] document prefilling initialstate --- docs/api/createEntityAdapter.mdx | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/docs/api/createEntityAdapter.mdx b/docs/api/createEntityAdapter.mdx index be14acc886..d08d145bc9 100644 --- a/docs/api/createEntityAdapter.mdx +++ b/docs/api/createEntityAdapter.mdx @@ -274,6 +274,39 @@ const booksSlice = createSlice({ }) ``` +You can also pass in an array of entities or a `Record` 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: