Skip to content

Commit 2c34e07

Browse files
authored
Merge pull request #2100 from reduxjs/feature/9.0-react-native
Add an RN-specific bundle and consolidate imports
2 parents 8a20779 + e5a558a commit 2c34e07

14 files changed

+57
-14
lines changed

Diff for: .github/workflows/test.yml

-4
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ jobs:
121121
- name: Clone RTK repo
122122
run: git clone https://github.com/reduxjs/redux-toolkit.git ./redux-toolkit
123123

124-
- name: Check out v2.0-integration
125-
working-directory: ./redux-toolkit
126-
run: git checkout v2.0-integration
127-
128124
- name: Check folder contents
129125
run: ls -l .
130126

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
"bugs": "https://github.com/reduxjs/react-redux/issues",
1515
"module": "dist/react-redux.legacy-esm.js",
1616
"main": "dist/cjs/index.js",
17+
"react-native": "./dist/react-redux.react-native.mjs",
1718
"types": "dist/react-redux.d.ts",
1819
"exports": {
1920
"./package.json": "./package.json",
2021
".": {
2122
"types": "./dist/react-redux.d.ts",
2223
"react-server": "./dist/rsc.mjs",
24+
"react-native": "./dist/react-redux.react-native.mjs",
2325
"import": "./dist/react-redux.mjs",
2426
"default": "./dist/cjs/index.js"
2527
},
@@ -50,7 +52,7 @@
5052
"react": "^18.0",
5153
"react-dom": "^18.0",
5254
"react-native": ">=0.71",
53-
"redux": "^5.0.0-rc"
55+
"redux": "^5.0.0"
5456
},
5557
"peerDependenciesMeta": {
5658
"@types/react": {

Diff for: src/alternate-renderers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Examples include React-Three-Fiber, Ink, etc.
44
// We'll assume they're built with React 18 and thus have `useSyncExternalStore` available.
55

6-
import * as React from 'react'
6+
import { React } from './utils/react'
77
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector.js'
88

99
import { initializeUseSelector } from './hooks/useSelector'

Diff for: src/components/Context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Context } from 'react'
2-
import * as React from 'react'
2+
import { React } from '../utils/react'
33
import type { Action, Store, UnknownAction } from 'redux'
44
import type { Subscription } from '../utils/Subscription'
55
import type { ProviderProps } from './Provider'

Diff for: src/components/Provider.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Context, ReactNode } from 'react'
2-
import * as React from 'react'
2+
import { React } from '../utils/react'
33
import type { Action, Store, UnknownAction } from 'redux'
44
import type { DevModeCheckFrequency } from '../hooks/useSelector'
55
import { createSubscription } from '../utils/Subscription'

Diff for: src/components/connect.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable valid-jsdoc, @typescript-eslint/no-unused-vars */
22
import type { ComponentType } from 'react'
3-
import * as React from 'react'
3+
import { React } from '../utils/react'
44
import { isValidElementType, isContextConsumer } from '../utils/react-is'
55

66
import type { Store } from 'redux'

Diff for: src/hooks/useReduxContext.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react'
1+
import { React } from '../utils/react'
22
import { ReactReduxContext } from '../components/Context'
33
import type { ReactReduxContextValue } from '../components/Context'
44

Diff for: src/hooks/useSelector.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as React from 'react'
1+
//import * as React from 'react'
2+
import { React } from '../utils/react'
23

34
import type { ReactReduxContextValue } from '../components/Context'
45
import { ReactReduxContext } from '../components/Context'

Diff for: src/react-native.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// The primary entry point assumes we are working with React 18, and thus have
2+
// useSyncExternalStore available. We can import that directly from React itself.
3+
// The useSyncExternalStoreWithSelector has to be imported, but we can use the
4+
// non-shim version. This shaves off the byte size of the shim.
5+
6+
import { React } from './utils/react'
7+
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector.js'
8+
9+
import { unstable_batchedUpdates as batchInternal } from './utils/reactBatchedUpdates.native'
10+
import { setBatch } from './utils/batch'
11+
12+
import { initializeUseSelector } from './hooks/useSelector'
13+
import { initializeConnect } from './components/connect'
14+
15+
initializeUseSelector(useSyncExternalStoreWithSelector)
16+
initializeConnect(React.useSyncExternalStore)
17+
18+
// Enable batched updates in our subscriptions for use
19+
// with standard React renderers (ReactDOM, React Native)
20+
setBatch(batchInternal)
21+
22+
// Avoid needing `react-dom` in the final TS types
23+
// by providing a simpler type for `batch`
24+
const batch: (cb: () => void) => void = batchInternal
25+
26+
export { batch }
27+
28+
export * from './exports'

Diff for: src/utils/react.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as ReactOriginal from 'react'
2+
import type * as ReactNamespace from 'react'
3+
4+
export const React: typeof ReactNamespace =
5+
// prettier-ignore
6+
// @ts-ignore
7+
'default' in ReactOriginal ? ReactOriginal['default'] : ReactOriginal as any

Diff for: src/utils/useIsomorphicLayoutEffect.native.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react'
1+
import { React } from '../utils/react'
22

33
// Under React Native, we know that we always want to use useLayoutEffect
44

Diff for: src/utils/useIsomorphicLayoutEffect.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react'
1+
import { React } from '../utils/react'
22

33
// React currently throws a warning when using useLayoutEffect on the server.
44
// To get around it, we can conditionally useEffect on the server (no-op) and

Diff for: tsup.config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ export default defineConfig((options) => {
7676
format: ['esm'],
7777
outExtension: () => ({ js: '.mjs' }),
7878
},
79+
// React Native requires a separate entry point for `"react-native"` batch dep
80+
{
81+
...commonOptions,
82+
entry: {
83+
'react-redux.react-native': 'src/react-native.ts',
84+
},
85+
format: ['esm'],
86+
outExtension: () => ({ js: '.mjs' }),
87+
},
7988
// CJS development
8089
{
8190
...commonOptions,

Diff for: yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -9417,7 +9417,7 @@ __metadata:
94179417
react: ^18.0
94189418
react-dom: ^18.0
94199419
react-native: ">=0.71"
9420-
redux: ^5.0.0-rc
9420+
redux: ^5.0.0
94219421
peerDependenciesMeta:
94229422
"@types/react":
94239423
optional: true

0 commit comments

Comments
 (0)