# createStore

```javascript
const { useStore, dispatch, query, fetch } = createStore({
  state: {},
  reducers: {},
  affects: {},
})
```

`state:any`

初始化的 state，建议给 state 定义类型，当然这不是必须的，不定义类型会自动推倒出 state 的类型。

```typescript
interface State {
  count: number
  step?: number
}

const initialState: State = { count: 0 } as State
const someStore = createStore({ state: initialState })
```

`reducers: { [string]: (state, payload) => any }`

Dahalia 中有两种类型的 action：reducer 和 effect，你只能通过 reducers 里面的 action 更新 state，如果是要在 action 中进行副作用操作，请使用 effects。

```javascript
const someStore = createStore({
  reducers: {
    increment(state, step) {
      state.count += step
    },
  },
})
```

`effects: { [string]: (payload) => any }`

如果是要在 action 中进行副作用操作，请使用 effects，比如异步网络请求、定时器等。需要改变 state，你需要 dispatch 一个 action 。

```javascript
const { dispatch } = createStore({
  effects: {
    async asyncIncrement() {
      await new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve()
        }, 1000)
      })
      dispatch(A => A.increment)
    },
  },
})
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dahlia-cn.gitbook.io/dahlia/api/createstore.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
