Skip to content

Commit a789c28

Browse files
committed
fix: better react component detection
- remove use of `react-is` (bundling error with rollup) - user special `$$typeof` to detect `Context.Provider`, `Context.Consumer` and object returned by `React.forwardRef`
1 parent 7080537 commit a789c28

6 files changed

Lines changed: 75 additions & 34 deletions

File tree

.size-snapshot.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
{
2-
"dist/react-render-callback.cjs.js": {
3-
"bundled": 1810,
4-
"minified": 1035,
5-
"gzipped": 475
2+
"dist/react-render-callback.umd.js": {
3+
"bundled": 3153,
4+
"minified": 1305,
5+
"gzipped": 622
66
},
77
"dist/react-render-callback.esm.js": {
8-
"bundled": 1654,
9-
"minified": 922,
10-
"gzipped": 424,
8+
"bundled": 2422,
9+
"minified": 1315,
10+
"gzipped": 579,
1111
"treeshaked": {
1212
"rollup": {
13-
"code": 78,
14-
"import_statements": 78
13+
"code": 269,
14+
"import_statements": 101
1515
},
1616
"webpack": {
17-
"code": 1128
17+
"code": 1316
1818
}
1919
}
2020
},
21-
"dist/react-render-callback.umd.min.js": {
22-
"bundled": 7335,
23-
"minified": 3333,
24-
"gzipped": 1160
21+
"dist/react-render-callback.cjs.js": {
22+
"bundled": 2579,
23+
"minified": 1432,
24+
"gzipped": 617
2525
},
26-
"dist/react-render-callback.umd.js": {
27-
"bundled": 11850,
28-
"minified": 4930,
29-
"gzipped": 1417
26+
"dist/react-render-callback.umd.min.js": {
27+
"bundled": 3186,
28+
"minified": 1291,
29+
"gzipped": 611
3030
}
3131
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
"node": ">=8.0.0"
3333
},
3434
"peerDependencies": {
35-
"react": ">=0.14.9"
35+
"react": "^0.14.0 || ^15.0.0-0 || ^16.0.0-0"
3636
},
3737
"dependencies": {
38-
"@babel/runtime-corejs2": "^7.0.0",
39-
"react-is": "^16.4.2"
38+
"@babel/runtime-corejs2": "^7.0.0"
4039
},
4140
"devDependencies": {
4241
"@babel/plugin-transform-runtime": "^7.0.0",

rollup.config.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@ module.exports = () => {
33

44
const rollupConfig = getRollupConfig()
55

6-
replace(rollupConfig.plugins, 'commonjs', () => {
7-
const commonjs = require('rollup-plugin-commonjs')
8-
9-
return commonjs({
10-
include: 'node_modules/**',
11-
namedExports: {
12-
'react-is': ['isValidElementType'],
13-
},
14-
})
15-
})
16-
176
if (process.env.BUILD_FORMAT !== 'umd') {
187
replace(rollupConfig.plugins, 'babel', () => {
198
const babel = require('rollup-plugin-babel')

src/internal/isReactComponent.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import {isValidElementType} from 'react-is'
1+
import {
2+
REACT_PROVIDER_TYPE,
3+
REACT_CONTEXT_TYPE,
4+
REACT_FORWARD_REF_TYPE,
5+
} from './reactSymbols'
26

37
export default renderable => {
8+
if (renderable == null) return false
9+
410
const type = typeof renderable
511

612
if (type === 'string') return false
@@ -13,5 +19,10 @@ export default renderable => {
1319
)
1420
}
1521

16-
return isValidElementType(renderable)
22+
return (
23+
type === 'object' &&
24+
(renderable.$$typeof === REACT_PROVIDER_TYPE ||
25+
renderable.$$typeof === REACT_CONTEXT_TYPE ||
26+
renderable.$$typeof === REACT_FORWARD_REF_TYPE)
27+
)
1728
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
3+
React.createContext = undefined
4+
React.forwardRef = undefined
5+
6+
let reactSymbols
7+
8+
beforeAll(async () => {
9+
reactSymbols = await import('./reactSymbols')
10+
})
11+
12+
test('react symbols before v16.3.0', () => {
13+
expect(reactSymbols).toMatchInlineSnapshot(`
14+
Object {
15+
"REACT_CONTEXT_TYPE": undefined,
16+
"REACT_FORWARD_REF_TYPE": undefined,
17+
"REACT_PROVIDER_TYPE": undefined,
18+
}
19+
`)
20+
})

src/internal/reactSymbols.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {createContext, forwardRef} from 'react'
2+
3+
// React.createContext is available since v16.3.0
4+
const context =
5+
/*#__PURE__*/ typeof createContext === 'function'
6+
? createContext()
7+
: {Provider: {}, Consumer: {}}
8+
9+
export const REACT_PROVIDER_TYPE = /*#__PURE__*/ context.Provider.$$typeof
10+
11+
export const REACT_CONTEXT_TYPE = /*#__PURE__*/ context.Consumer.$$typeof
12+
13+
// React.forwardRef is available since v16.3.0
14+
export const REACT_FORWARD_REF_TYPE =
15+
/*#__PURE__*/ typeof forwardRef === 'function'
16+
? forwardRef(
17+
// need to access both params otherwise react warns
18+
// but as this is never used this is no-op
19+
/* istanbul ignore next line */
20+
(props, ref) => ({props, ref}),
21+
).$$typeof
22+
: undefined

0 commit comments

Comments
 (0)