Skip to main content
On this pageOverview

Update

Overview

The update function is the heart of your application logic. It's a pure function that takes the current model and a message, and returns a new model along with any commands to execute. Commands represent side effects and are covered later on this page.

Foldkit uses Effect.Match for exhaustive pattern matching on messages. The TypeScript compiler will error if you forget to handle a message type.

import { Match as M } from 'effect'
import { Command } from 'foldkit/command'

// UPDATE - How your state changes in response to messages
// Returns a tuple of [nextModel, commands]
// Commands are side effects like HTTP requests (none needed here)

const update = (
  model: Model,
  message: Message,
): [Model, ReadonlyArray<Command<Message>>] =>
  M.value(message).pipe(
    M.withReturnType<[Model, ReadonlyArray<Command<Message>>]>(),
    M.tagsExhaustive({
      // This means: the next model (application state) is
      // model - 1 and there are no commands to run
      ClickedDecrement: () => [model - 1, []],
      ClickedIncrement: () => [model + 1, []],
      ClickedReset: () => [0, []],
    }),
  )