polemius / recoil-persist

Package for recoil state manager to persist and rehydrate store
https://polemius.dev/recoil-persist/
MIT License
353 stars 39 forks source link

Hydration Issue nextjs #65

Open Leykwan132 opened 2 years ago

Leykwan132 commented 2 years ago

https://stackoverflow.com/questions/68110629/nextjs-react-recoil-persist-values-in-local-storage-initial-page-load-in-wrong/73536131?noredirect=1#comment129857349_73536131

I've gone through this question but the value stored in the state of my atom is an object that is returned by an API call which is relatively complex compared to the boolean value mentioned in the stackoverflow question above. Unlike the case above, I don't think I can set the value of SSR to be the same as the CSR. How should I handle the hydration? Or can I just ignore the issue? WIll it impact the website when it is deployed to production?

zero734kr commented 2 years ago

Any progress here? Personally I ended up moving the recoil state call to another wrapper component and forced to CSR with next/dynamic like below:

import dynamic from 'next/dynamic'

// ... 
const SomeRecoilStateWrapper = dynamic(
  () => Promise.resolve(SomeRecoilStateContainer),
  { ssr: false }
)

function SomeRecoilStateContainer () {
  const value = useRecoilValue(...) // recoil atom using recoil-persist

  // ...
}

But I would really like to see a better solution here

RizqiSyahrendra commented 1 year ago

I thought this kind of problem has been fixed on #24 , don't know why this problem has still appeared now

joacub commented 1 year ago

there is a missmatch error using recoil, when you use concurrent mode or startTransition, recoil-persist get hydrated before all render and this does not match with server at all ofcourse

kensoz commented 1 year ago

same here. in my case atom is an Array.

Here is a simplified code, and sorry for my bad English. When the button is clicked, the ID of the clicked item will be saved to atom recoilTest array. Then the icon below will determine whether to display a star or a heart according to the ID in atom recoilTest array. Click, then refresh the page, there will be a Hydration Issue.

.tsx

...
<button type='button' onClick={() => onClick( {id: e.id} )}>
  <FontAwesomeIcon icon={recoilTest.findIndex((i) => i.id === e.id) !== -1 ? faStar : faHeart} />
</button>
...

recoil.ts

const { persistAtom } = recoilPersist()

export const recoilTest = atom({
  key: 'recoilTest',
  default: [{id: '1'}],
  effects_UNSTABLE: [persistAtom],
})

next: 12.3.1 react: 18.2.0 recoil: ^0.7.5 recoil-persist: ^4.2.0

joshephan commented 1 year ago

same here.

this issue has been quite long nobody answer :(

is recoil project dead?

polemius commented 1 year ago

I have added a section to README