combine.md
This file
Path
docs/reference/middlewares/combine.md
103 lines · 2.7 KiB · Markdown
Last changed by
docs(combine): add object constraints to T and U in signature (#3506)seunghyunbefore adoptiond690ec2
History of this fileTake it
Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
lines 1–21why, one step back1---2title: combine3description: How to create a store and get types automatically inferred4nav: 325---67# combine89`combine` middleware lets you create a cohesive state by merging an initial state with a state10creator function that adds new state slices and actions. This is really helpful as it automatically11infers types, so there’s no need for explicit type definitions.1213> [!TIP]14> This makes state management more straightforward and efficient by making curried version of15> `create` and `createStore` not necessary for middleware usage.1617```js18const nextStateCreatorFn = combine(initialState, additionalStateCreatorFn)19```2021- [Types](#types)Hotfix section linking (#3410)Copilotbefore adoptionc49df38
line 22why, one step back22 - [Signature](#signature)Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
lines 23–24why, one step back23- [Reference](#reference)24- [Usage](#usage)Hotfix section linking (#3410)Copilotbefore adoptionc49df38
line 25why, one step back25 - [Creating a store with inferred types](#creating-a-store-with-inferred-types)Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
lines 26–32why, one step back26- [Troubleshooting](#troubleshooting)2728## Types2930### Signature3132```tsdocs(combine): add object constraints to T and U in signature (#3506)seunghyunbefore adoptiond690ec2
line 33why, one step back33combine<T extends object, U extends object>(initialState: T, additionalStateCreatorFn: StateCreator<T, [], [], U>): StateCreator<Omit<T, keyof U> & U, [], []>Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
lines 34–103why, one step back34```3536## Reference3738### `combine(initialState, additionalStateCreatorFn)`3940#### Parameters4142- `initialState`: The value you want the state to be initially. It can be a value of any type,43 except a function.44- `additionalStateCreatorFn`: A function that takes `set` function, `get` function and `store` as45 arguments. Usually, you will return an object with the methods you want to expose.4647#### Returns4849`combine` returns a state creator function.5051## Usage5253### Creating a store with inferred types5455This example shows you how you can create a store and get types automatically inferred, so you56don’t need to define them explicitly.5758```ts59import { createStore } from 'zustand/vanilla'60import { combine } from 'zustand/middleware'6162const positionStore = createStore(63 combine({ position: { x: 0, y: 0 } }, (set) => ({64 setPosition: (position) => set({ position }),65 })),66)6768const $dotContainer = document.getElementById('dot-container') as HTMLDivElement69const $dot = document.getElementById('dot') as HTMLDivElement7071$dotContainer.addEventListener('pointermove', (event) => {72 positionStore.getState().setPosition({73 x: event.clientX,74 y: event.clientY,75 })76})7778const render: Parameters<typeof positionStore.subscribe>[0] = (state) => {79 $dot.style.transform = `translate(${state.position.x}px, ${state.position.y}px)`80}8182render(positionStore.getInitialState(), positionStore.getInitialState())8384positionStore.subscribe(render)85```8687Here's the `html` code8889```html90<div91 id="dot-container"92 style="position: relative; width: 100vw; height: 100vh;"93>94 <div95 id="dot"96 style="position: absolute; background-color: red; border-radius: 50%; left: -10px; top: -10px; width: 20px; height: 20px;"97 ></div>98</div>99```100101## Troubleshooting102103TBD