Skip to content

Commit

Permalink
convert initialEvents to a function
Browse files Browse the repository at this point in the history
Ensure that hydrateClientStorage is being called fresh each time the page is
hydrated to allow recently set local storage / cookie vars to be sent to the
backend.

Fix #1979
  • Loading branch information
masenf committed Oct 16, 2023
1 parent a0df1a8 commit 3cca45b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion reflex/.templates/jinja/web/pages/index.js.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function Component() {

// Route after the initial page hydration.
useEffect(() => {
const change_complete = () => addEvents(initialEvents.map((e) => ({...e})))
const change_complete = () => addEvents(initialEvents())
{{const.router}}.events.on('routeChangeComplete', change_complete)
return () => {
{{const.router}}.events.off('routeChangeComplete', change_complete)
Expand Down
2 changes: 1 addition & 1 deletion reflex/.templates/jinja/web/utils/context.js.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export const initialState = {{ initial_state|json_dumps }}
export const StateContext = createContext(null);
export const EventLoopContext = createContext(null);
export const clientStorage = {{ client_storage|json_dumps }}
export const initialEvents = [
export const initialEvents = () => [
Event('{{state_name}}.{{const.hydrate}}', hydrateClientStorage(clientStorage)),
]
6 changes: 3 additions & 3 deletions reflex/.templates/web/utils/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ const applyClientStorageDelta = (client_storage, delta) => {
/**
* Establish websocket event loop for a NextJS page.
* @param initial_state The initial app state.
* @param initial_events The initial app events.
* @param initial_events Function that returns the initial app events.
* @param client_storage The client storage object from context.js
*
* @returns [state, addEvents, connectError] -
Expand All @@ -478,7 +478,7 @@ const applyClientStorageDelta = (client_storage, delta) => {
*/
export const useEventLoop = (
initial_state = {},
initial_events = [],
initial_events = () => [],
client_storage = {},
) => {
const socket = useRef(null)
Expand All @@ -496,7 +496,7 @@ export const useEventLoop = (
// initial state hydrate
useEffect(() => {
if (router.isReady && !sentHydrate.current) {
addEvents(initial_events.map((e) => ({ ...e })))
addEvents(initial_events())
sentHydrate.current = true
}
}, [router.isReady])
Expand Down

0 comments on commit 3cca45b

Please sign in to comment.