Skip to content

Commit 63f626b

Browse files
committed
Comment areas of concern for connect + uSES
1 parent bc4569f commit 63f626b

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
@@ -27,7 +27,10 @@ import defaultMapStateToPropsFactories from '../connect/mapStateToProps'
2727
import defaultMergePropsFactories from '../connect/mergeProps'
2828

2929
import { createSubscription, Subscription } from '../utils/Subscription'
30-
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
30+
import {
31+
useIsomorphicLayoutEffect,
32+
canUseDOM,
33+
} from '../utils/useIsomorphicLayoutEffect'
3134
import shallowEqual from '../utils/shallowEqual'
3235

3336
import {
@@ -124,6 +127,7 @@ function subscribeUpdates(
124127
return
125128
}
126129

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

129133
let newChildProps, error
@@ -157,6 +161,7 @@ function subscribeUpdates(
157161
childPropsFromStoreUpdate.current = newChildProps
158162
renderIsScheduled.current = true
159163

164+
// TODO This is hacky and not how `uSES` is meant to be used
160165
// Trigger the React `useSyncExternalStore` subscriber
161166
additionalSubscribeListener()
162167
}
@@ -597,6 +602,10 @@ function connect<
597602
? props.store!
598603
: contextValue!.store
599604

605+
const getServerSnapshot = didStoreComeFromContext
606+
? contextValue.getServerState
607+
: store.getState
608+
600609
const childPropsSelector = useMemo(() => {
601610
// The child props selector needs the store reference as an input.
602611
// Re-create this selector whenever the store changes.
@@ -724,7 +733,10 @@ function connect<
724733

725734
try {
726735
actualChildProps = useSyncExternalStore(
736+
// TODO We're passing through a big wrapper that does a bunch of extra side effects besides subscribing
727737
subscribeForReact,
738+
// TODO This is incredibly hacky. We've already processed the store update and calculated new child props,
739+
// TODO and we're just passing that through so it triggers a re-render for us rather than relying on `uSES`.
728740
actualChildPropsSelector,
729741
// TODO Need a real getServerSnapshot here
730742
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)