-
Beta Was this translation helpful? Give feedback.
Answered by
IonelLupu
Jan 26, 2024
Replies: 3 comments
-
please show a minimal runnable reproduction. There shouldn't be a hydration mismatch with that code. |
Beta Was this translation helpful? Give feedback.
0 replies
-
did you fix it ? |
Beta Was this translation helpful? Give feedback.
0 replies
-
@b4ljk yes. I had to create a special hook for retrieving the data in an async way. Not ideal, but does the job: /**
* Use this function when you want to fix the "hydration" error in NextJS
* @param store
* @param callback
*/
export const useAsyncStore = <T, F>(store: (callback: (state: T) => unknown) => unknown, callback: (state: T) => F) => {
const result = store(callback) as F
const [data, setData] = useState<F>()
useEffect(() => {
setData(typeof result === 'function' ? () => result : result)
}, [result])
return data
}
/**
* Use this function when you want to fix the "hydration" error in NextJS
* @param key
*/
export const useAsyncStoreValue = <K extends keyof ContextData>(key: K) => {
return useAsyncStore(yourContextNameHere, state => state[key])
} And in my components I just use it like this: function CurrencySelector() {
const currencyCode = useAsyncStoreValue('currencyCode')
return <>{currencyCode}</>
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
IonelLupu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@b4ljk yes. I had to create a special hook for retrieving the data in an async way. Not ideal, but does the job: