Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Missing getServerSnapshot, which is required for server-rendered content. Will revert to client rendering. #886

Closed
talentlessguy opened this issue Mar 30, 2022 · 5 comments

Comments

@talentlessguy
Copy link
Member

With React 18, this error occurs when using a useStore hook:

// useDarkMode.ts
import create from 'zustand'
import { persist } from 'zustand/middleware'

type darkModeState = {
  /**
   * State of user settings
   */
  isDarkMode: boolean
  toggleDarkMode: () => void
}

export const useDarkMode = create<darkModeState>(
  persist(
    (set) => ({
      isDarkMode: false,
      toggleDarkMode: () =>
        set(({ isDarkMode }) => ({
          isDarkMode: !isDarkMode
        }))
    }),
    { name: 'useDarkMode' }
  )
)
// app.tsx

import { useMemo } from 'react'
import { cssStringFromTheme } from '../css/cssStringFromTheme'
import { useDarkMode } from '../hooks/useDarkMode'
import { darkTheme } from '../themes/darkTheme'
import { lightTheme } from '../themes/lightTheme'
import { Box } from './Box'

export const ThemeSwitch = () => {
  const isDarkMode = useDarkMode((s) => s.isDarkMode)
  const toggleDarkMode = useDarkMode((s) => s.toggleDarkMode)

  const themeObject = useMemo(() => {
    if (isDarkMode) return darkTheme
    else return lightTheme
  }, [isDarkMode])

  return (
    <style global jsx>
      {`
        :root {
          ${cssStringFromTheme(themeObject)}
        }

        @media (prefers-color-scheme: dark) {
          :root {
            ${cssStringFromTheme(isDarkMode ? darkTheme : themeObject)}
          }
        }
      `}
    </style>
  )
}

this error occurs:

image

This is using zustand@4.0.0-beta.2, react@18 and next@12.1

@dai-shi
Copy link
Member

dai-shi commented Mar 31, 2022

Ohhh, I wasn't aware that.. I was expecting it will revert to client rendering silently.
Thanks for reporting.
Is this warning only problem? Hmm, I think we should follow how react-redux does. 🤔

dai-shi added a commit that referenced this issue Mar 31, 2022
@dai-shi
Copy link
Member

dai-shi commented Mar 31, 2022

Can you try this?
https://ci.codesandbox.io/status/pmndrs/zustand/pr/550/builds/233894
Find "Local Install Instructions" ☝️

@talentlessguy
Copy link
Member Author

talentlessguy commented Mar 31, 2022

yeah it seems to work, thanks

https://codesandbox.io/s/react-typescript-forked-7fyb80?file=/src/App.tsx

looking forward to have this in 4.0.0-beta.3

@dai-shi
Copy link
Member

dai-shi commented Apr 5, 2022

@dai-shi dai-shi closed this as completed Apr 5, 2022
dai-shi added a commit that referenced this issue Apr 7, 2022
* imaginary code that uses uSES

* revert backward compatibility code as this is not going to be v4

* use use-sync-external-store

* revert to react 17

* handle error by our own

* v4.0.0-alpha.2

* fix&refactor a bit

* update uSES experimental package

* remove error propagation hack

* update size snapshot

* update uSES and add dts

* split react.ts and no export wild

* split useStore impl

* context to follow the new api, export wild again

* v4.0.0-alpha.3

* add missing await

* update uSES

* update uSES

* uses uSES extra!

* v4.0.0-alpha.3

* update uSES

* fix update uSES

* v4.0.0-alpha.5

* add useDebugValue

* update uSES

* update uSES types

* update uSES

* v4.0.0-alpha.6

* fix(readme): remove memoization section which is no longer valid with uSES

* feat(readme): add new createStore/useStore usage

* update useSES

* update uSES and deps

* v4.0.0-alpha.7

* update uSES

* update uSES

* shave bytes

* update uSES

* fix yarn lock

* temporary fix #829

* uSES rc.1

* getServerState for #886, no types yet

* uSES v1
dai-shi added a commit that referenced this issue Apr 18, 2022
* imaginary code that uses uSES

* revert backward compatibility code as this is not going to be v4

* use use-sync-external-store

* revert to react 17

* handle error by our own

* v4.0.0-alpha.2

* fix&refactor a bit

* update uSES experimental package

* remove error propagation hack

* update size snapshot

* update uSES and add dts

* split react.ts and no export wild

* split useStore impl

* context to follow the new api, export wild again

* v4.0.0-alpha.3

* add missing await

* update uSES

* update uSES

* uses uSES extra!

* v4.0.0-alpha.3

* fix(types): Rename from UseStore to UseBoundStore

* breaking(types): drop deprecated UseStore type

* breaking(core): drop v2 hook compatibility

* breaking(middleware): drop deprecated persist options

* breaking(core): drop deprecated store.subscribe with selector

* update uSES

* fix update uSES

* v4.0.0-alpha.5

* combine subscribe type

* intentional undefined type

* add useDebugValue

* update uSES

* update uSES types

* breaking(middleware): make persist options.removeItem required

* update uSES

* v4.0.0-alpha.6

* fix(readme): remove memoization section which is no longer valid with uSES

* feat(readme): add new createStore/useStore usage

* update useSES

* update uSES and deps

* v4.0.0-alpha.7

* update uSES

* update uSES

* shave bytes

* vanilla: add higher kinded mutator types

* persist: add higher kinded mutator types

* persist: try to minimize diff

* use `PopArgument` in vanilla too

* update uSES

* use overloads instead of `createWithState`

* avoid symbols

* add new types to middlewares

* add new types react

* add new types to context

* fix persist types

* add immer

* migrate middleware type tests

* fix react type, export `UseBoundStore`

* migrate vanilla type tests

* rename `_createStore` to `createStoreImpl`

* Default to no mutations in `StateCreator`

* migrate context.test.tsx

* fix devtools.test.tsx type erros

* context: remove callsignature in useStoreApi

* context: remove `UseContextStore` type

* context: fix useBoundStore type

* context: keep `UseContextStore` for tooltip just don't export it

* react: remove duplicate overload in create

* export `WithPersist`

* devtools: preserve try/catch

* devtools: preserve window check

* add a test case for v3 style create

* devtools: preverse test fix from base branch

* remove StoreApiWithFoo types, don't export WithFoo types

* style

* devtools: preverse `originalIsRecording` change

* fix bug in devtools

* empty commit

* 4.0.0-beta.1

* fix lint

* style

* export immer fix tests

* style, minor fixes

* devtools: fix test

* Update tests/devtools.test.tsx

* breaking(middleware/devtools): use official devtools extension types

* type object.create

* avoid emitting @redux-devtools/extension

* fix type with any

* refactor

* fix yarn lock

* temporary fix #829

* v4.0.0-beta.2

* fix lint

* lock date-fns version

* test middleware subtyping

* fix errors in conflict resolution

* lock testing-library/react alpha version

* more correct (and strict) persist types

* migrate tests

* wip release notes

* fix devtools merge with base

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>

* add a test case for persist with partialize option

* update readme

* fix lint

* immer: mutate `store.setState`

* fix devtools merge with base

* immer: fix mutations order

* changes in readme

* move and rename v4 migration md

* add `combine` usage in readme

* typos

* create separate md for typescript, add common recipes

* minor fixes

* devtools: minor type fix
(probably I copy pasted from persist and forgot to remove `U`)

* add more migrations

* context: fix import

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>

* devtools: minor refactor
rearrange code for better diff

* fix lint: run prettier

* wip

* uSES rc.1

* getServerState for #886, no types yet

* uSES v1

* devtools: remove deprecations and warnings

* fix tests

* v4.0.0-beta.2

* wip

* migrate tests

* persist: keep diff minimal

* fix merge in package.json and yarn.lock

* fix merge for persist

* don't use `import type`

* docs(typescript): add slices pattern

* fix selector & equals types
for inference with useCallback, see issue #812

* add test for setState with replace

* remove undefined selector overload

* make immer more generic

* make devtools replace-friendly and more correctly typed

* migrate tests

* make setState bivariant to make the state covariant

* devtools: return the result of `setState`

* devtools: make the fallback branch in `StoreSetStateWithAction` bivariant too
(I forgot to make this bivaraint)

* remove strict replace

* fix lint

Co-authored-by: daishi <daishi@axlight.com>
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
@nickytonline
Copy link

I seem to be experiencing this issue in open-sauced/app#1734 with Zustand 4.3.2 with Next.js 13.4.7. Still digging into why the fix here doesn't kick in. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants