ilokesto

create

각 프레임워크 subpath는 create를 내보냅니다. 반환 형태는 프레임워크마다 다르지만 overload 개념은 같습니다.

Plain state overload

create<T>(initialState: T | Store<T>): UseState<T>

const useSession = create({ user: null as null | { id: string } });

plain state는 setState라는 writer 또는 프레임워크 친화적인 equivalent를 제공합니다. React는 tuple을 반환하고, Svelte는 set, update, setState를 노출하며, Vue, Solid, Angular는 state와 setState를 가진 object를 반환합니다.

Reducer overload

create<T, Action extends { readonly type: string }>(
  reduceFn: (state: T, action: Action) => T,
  initialState: T | Store<T>,
): UseReducer<T, Action>

reducer state는 dispatch 또는 action을 받는 writeOnly() writer를 제공합니다. 내부적으로 action은 middleware에서 state로 변환된 뒤 store pipeline을 계속 통과합니다.

기존 Store 재사용하기

import { Store } from '@ilokesto/store';
import { create } from '@ilokesto/state/react';

const store = new Store({ count: 0 });
const useCounter = create(store);

여러 adapter 또는 프레임워크 밖 코드가 하나의 source of truth를 공유해야 한다면 기존의 구체 Store<T>를 넘기세요. create는 값 또는 Store<T>를 받으며, 모든 구조적 StoreApi<T>를 받는 것은 아닙니다.

명시적 binding

모든 프레임워크 subpath는 bind(store: StoreApi<T>)와 bindReducer(handle)도 내보냅니다. create와 달리 bind는 구조적 store를 받고 그 값을 해석하거나 새 store를 만들지 않습니다. bindReducer는 root createReducer가 반환한 { store, dispatch }를 받아 reducer를 다시 등록하지 않습니다.

계약은 프레임워크 독립 상태와 연결에서, 이전 메이저에서 바뀐 내용은 이전 가이드에서 확인하세요.

readOnly()와 writeOnly()는 읽기와 쓰기 API에서, 작업 중심 예제는 plain state 가이드와 reducer state 가이드에서 이어서 확인하세요.

목차