Skip to content

Commit 4332de3

Browse files
committed
Comment areas of concern for connect + uSES
1 parent 595e2b6 commit 4332de3

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/components/connect.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ import defaultMapStateToPropsFactories from '../connect/mapStateToProps'
3434
import defaultMergePropsFactories from '../connect/mergeProps'
3535

3636
import { createSubscription, Subscription } from '../utils/Subscription'
37-
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
37+
import {
38+
useIsomorphicLayoutEffect,
39+
canUseDOM,
40+
} from '../utils/useIsomorphicLayoutEffect'
3841
import shallowEqual from '../utils/shallowEqual'
3942

4043
import {
@@ -123,6 +126,7 @@ function subscribeUpdates(
123126
return
124127
}
125128

129+
// TODO We're currently calling getState ourselves here, rather than letting `uSES` do it
126130
const latestStoreState = store.getState()
127131

128132
let newChildProps, error
@@ -156,6 +160,7 @@ function subscribeUpdates(
156160
childPropsFromStoreUpdate.current = newChildProps
157161
renderIsScheduled.current = true
158162

163+
// TODO This is hacky and not how `uSES` is meant to be used
159164
// Trigger the React `useSyncExternalStore` subscriber
160165
additionalSubscribeListener()
161166
}
@@ -609,6 +614,10 @@ function connect<
609614
? props.store!
610615
: contextValue!.store
611616

617+
const getServerSnapshot = didStoreComeFromContext
618+
? contextValue.getServerState
619+
: store.getState
620+
612621
const childPropsSelector = useMemo(() => {
613622
// The child props selector needs the store reference as an input.
614623
// Re-create this selector whenever the store changes.
@@ -736,7 +745,10 @@ function connect<
736745

737746
try {
738747
actualChildProps = useSyncExternalStore(
748+
// TODO We're passing through a big wrapper that does a bunch of extra side effects besides subscribing
739749
subscribeForReact,
750+
// TODO This is incredibly hacky. We've already processed the store update and calculated new child props,
751+
// TODO and we're just passing that through so it triggers a re-render for us rather than relying on `uSES`.
740752
actualChildPropsSelector,
741753
// TODO Need a real getServerSnapshot here
742754
actualChildPropsSelector

src/utils/useIsomorphicLayoutEffect.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import { useEffect, useLayoutEffect } from 'react'
99
// is created synchronously, otherwise a store update may occur before the
1010
// subscription is created and an inconsistent state may be observed
1111

12-
export const useIsomorphicLayoutEffect =
12+
// Matches logic in React's `shared/ExecutionEnvironment` file
13+
export const canUseDOM = !!(
1314
typeof window !== 'undefined' &&
1415
typeof window.document !== 'undefined' &&
1516
typeof window.document.createElement !== 'undefined'
16-
? useLayoutEffect
17-
: useEffect
17+
)
18+
19+
export const useIsomorphicLayoutEffect = canUseDOM ? useLayoutEffect : useEffect

0 commit comments

Comments
 (0)