Skip to content

Conversation

@sukvvon
Copy link
Contributor

@sukvvon sukvvon commented Dec 3, 2025

Overview

Add HydrateAtoms component to @suspensive/jotai package.

This component is a wrapper around Jotai's useHydrateAtoms hook. It hydrates initial values into Jotai atoms, preventing state mismatch between server and client.

Features

props.atomValues

  • Array of tuples: [[atom1, value1], [atom2, value2]]
  • Map: new Map([[atom1, value1], [atom2, value2]])
  • Spread generator: [...generateAtomValues()]
  • Support as const for type inference
  • Support WritableAtom with multiple args: [[writableAtom, arg1, arg2]]

props.options

  • store: Specify a custom Jotai store to hydrate atoms into
  • dangerouslyForceHydrate: When true, forces re-hydration even if atoms have already been hydrated

props.children

  • The children will be rendered after atoms are hydrated

Example

Basic Usage

import { AtomValue, HydrateAtoms } from '@suspensive/jotai'
import { atom } from 'jotai'

const userIdAtom = atom('')

// Server Component
const Page = async () => {
  const userId = await getSessionUserId()

  return (
    <HydrateAtoms atomValues={[[userIdAtom, userId]]}>
      <AtomValue atom={userIdAtom}>
        {(userId) => <div>userId: {userId}</div>}
      </AtomValue>
    </HydrateAtoms>
  )
}

With Custom Store

import { AtomValue, HydrateAtoms } from '@suspensive/jotai'
import { atom, createStore } from 'jotai'
import { Provider } from 'jotai/react'

const userIdAtom = atom('')
const customStore = createStore()

// Server Component
const Page = async () => {
  const userId = await getSessionUserId()

  return (
    <Provider store={customStore}>
      <HydrateAtoms
        atomValues={[[userIdAtom, userId]]}
        options={{ store: customStore, dangerouslyForceHydrate: true }}
      >
        <AtomValue atom={userIdAtom}>
          {(userId) => <div>userId: {userId}</div>}
        </AtomValue>
      </HydrateAtoms>
    </Provider>
  )
}

With Suspense

import { Atom, HydrateAtoms } from '@suspensive/jotai'
import { Suspense } from '@suspensive/react'
import { atom } from 'jotai'

const userIdAtom = atom('')
const userDetailAtom = atom(async (get) => {
  const userId = get(userIdAtom)
  return await fetchUserDetail(userId)
})

// Server Component
const Page = async () => {
  const userId = await getSessionUserId()

  return (
    <HydrateAtoms atomValues={[[userIdAtom, userId]]}>
      <Suspense fallback={'pending...'}>
        <Atom atom={userDetailAtom}>{([user]) => <div>{user.name}</div>}</Atom>
      </Suspense>
    </HydrateAtoms>
  )
}

Changes

  • Add HydrateAtoms component (packages/jotai/src/HydrateAtoms.tsx)
  • Add runtime tests (packages/jotai/src/HydrateAtoms.spec.tsx) - 11 tests, 100% coverage
  • Add type tests (packages/jotai/src/HydrateAtoms.test-d.tsx)
  • Add documentation (English: docs/suspensive.org/src/content/en/docs/jotai/HydrateAtoms.mdx)
  • Add documentation (Korean: docs/suspensive.org/src/content/ko/docs/jotai/HydrateAtoms.mdx)
  • Export from packages/jotai/src/index.ts

Type Compatibility

Full type compatibility with jotai's useHydrateAtoms:

  • InferAtomTuples type for validating atom-value tuple types
  • Function overloads for Array, Map, and Iterable

PR Checklist

  • I did below actions if need
  1. I read the Contributing Guide
  2. I added documents and tests.

@changeset-bot
Copy link

changeset-bot bot commented Dec 3, 2025

🦋 Changeset detected

Latest commit: 4039a19

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@suspensive/jotai Minor
@suspensive/react Minor
@suspensive/react-dom Minor
@suspensive/react-query Minor
@suspensive/react-query-4 Minor
@suspensive/react-query-5 Minor
@suspensive/codemods Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coauthors
Copy link

coauthors bot commented Dec 3, 2025

People can be co-author:

Candidate Reasons Count Add this as commit message
@sukvvon #1855 1 Co-authored-by: sukvvon <[email protected]>
@codecov-commenter #1855 (comment) 1 Co-authored-by: codecov-commenter <[email protected]>

@vercel
Copy link

vercel bot commented Dec 3, 2025

@sukvvon is attempting to deploy a commit to the Toss Team on Vercel.

A member of the Team first needs to authorize it.

@codecov-commenter
Copy link

codecov-commenter commented Dec 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.56%. Comparing base (9965ec4) to head (4039a19).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1855      +/-   ##
==========================================
+ Coverage   93.53%   93.56%   +0.02%     
==========================================
  Files          45       46       +1     
  Lines         727      730       +3     
  Branches      185      184       -1     
==========================================
+ Hits          680      683       +3     
  Misses         41       41              
  Partials        6        6              
Components Coverage Δ
@suspensive/react 96.58% <ø> (ø)
@suspensive/react-dom 100.00% <ø> (ø)
@suspensive/react-query 100.00% <ø> (ø)
@suspensive/react-query-4 100.00% <ø> (ø)
@suspensive/react-query-5 100.00% <ø> (ø)
@suspensive/jotai 100.00% <100.00%> (ø)
@suspensive/codemods 81.60% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sukvvon sukvvon force-pushed the feat/jotai-add-hydrate-atoms branch from 33e7c8b to 780a694 Compare December 3, 2025 17:59
@sukvvon sukvvon marked this pull request as ready for review December 3, 2025 18:18
@sukvvon sukvvon requested a review from manudeli as a code owner December 3, 2025 18:18
@sukvvon sukvvon force-pushed the feat/jotai-add-hydrate-atoms branch 2 times, most recently from 05d23d1 to e079379 Compare December 3, 2025 18:40
@sukvvon sukvvon marked this pull request as draft December 4, 2025 00:59
@sukvvon sukvvon force-pushed the feat/jotai-add-hydrate-atoms branch from e079379 to 03aae54 Compare December 4, 2025 15:40
@sukvvon sukvvon marked this pull request as ready for review December 4, 2025 15:47
@sukvvon sukvvon force-pushed the feat/jotai-add-hydrate-atoms branch from 03aae54 to a84694c Compare December 4, 2025 15:51
@sukvvon sukvvon force-pushed the feat/jotai-add-hydrate-atoms branch from a84694c to bb05f3e Compare December 5, 2025 06:34
@gwansikk gwansikk self-requested a review December 10, 2025 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants