statekit • Page 6 of 14

Rerenders

17/10/2026 • 3 min read

Each component that calls a machine's hook subscribes to what it reads, and rerenders when one of those things changes. There are no selectors to write for it.

See it

One machine holds a post's stats, and four components read it. Each one reads something different and counts its renders. Like the post, add a view or type a note, and see which ones rerender:

Loading demo…

Without the render counts, the components are:

import {
   createMachine,
   createMachineContext,
} from "@statekit/react"

const statsMachine = createMachine({
   data: { likes: 6, views: 48, note: "Share on Friday" },
   events: {
      like: ({ draft }) => {
         draft.likes++
      },
      view: ({ draft }) => {
         draft.views++
      },
      setNote: ({ draft }, note: string) => {
         draft.note = note
      },
   },
   selectors: {
      likeRate: ({ data }) => {
         if (!data.views) return 0
         const rate = data.likes / data.views
         return Math.round(rate * 1000) / 10
      },
   },
})

const [useStatsMachine] =
   createMachineContext(statsMachine)

const Likes = () => {
   const m = useStatsMachine()
   return <p>{m.data.likes} likes</p>
}

const Views = () => {
   const m = useStatsMachine()
   return <p>{m.data.views} views</p>
}

const LikeRate = () => {
   const m = useStatsMachine()
   return <p>{m.select.likeRate()}% like rate</p>
}

const Summary = () => {
   const m = useStatsMachine()
   const { ...data } = m.data
   return (
      <p>
         {data.likes} likes, {data.views} views. {data.note}.
      </p>
   )
}
  • like rerenders Likes, and LikeRate, whose selector reads likes. Views never read likes, so it stays put.
  • view is the same for views, and leaves Likes alone.
  • A new note rerenders only Summary. Nothing else reads note, and the spread reads every field, so Summary rerenders on every change.

The rules

  • A component rerenders when a field it has read changes.
  • Reads add up for the life of the component. A field read once stays subscribed, and reads in event handlers count too.
  • Nested objects, arrays, object keys and array methods are tracked. m.data.user.name subscribes to that one field, and the rest of user can change without a rerender.
  • Reading m.state subscribes to state changes.
  • A selector subscribes to the fields it read.
  • Date, Map, Set, RegExp and Promise values are returned as they are. Only replacing one rerenders, so keep the data components read to plain objects, arrays and primitives.

Reading more than you need

Rest and array destructuring read the whole object:

const { name, ...rest } = m.data // all of data
const [first] = m.data.tags // every tag

const { name, email } = m.data // name and email
const first = m.data.tags[0] // the first tag

When a component does need the whole object, that's fine. When it doesn't, @statekit/eslint-plugin warns.

Object.keys(m) and { ...m } are empty on what the hook returns. Read m.data, m.event, m.select and m.state by name.

Outside React

A change made from a timer or a socket handler rerenders components the same way. The subscription is on what a component read, so where a change came from makes no difference.

#javascript, #npm, #react, #readme, #typescript, #web-development

Loading comments...