Skip to content

Commit 6fe5e6f

Browse files
committed
Fix forwardRef
1 parent 63f7187 commit 6fe5e6f

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

packages/react-scripts/template/src/CounterFunction.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import React, { memo, useReducer, useLayoutEffect } from 'react';
1+
import React, { forwardRef, memo, useReducer, useLayoutEffect } from 'react';
22
import HOCFunction from './HOCFunction';
33

44
let HFF;
55
let Counter;
6+
let Fwd;
67

78
Counter = window.__assign(
89
module,
@@ -18,7 +19,7 @@ Counter = window.__assign(
1819
}, []);
1920
return (
2021
<span>
21-
{value}{' '}
22+
{value}
2223
{props.hocChild && (
2324
<>
2425
(inner HOC: <HFF /> {HFF.field})
@@ -29,6 +30,15 @@ Counter = window.__assign(
2930
})
3031
)
3132
);
32-
HFF = window.__assign(module, 'HFF', HOCFunction(Counter));
33+
Fwd = window.__assign(
34+
module,
35+
'FWD',
36+
forwardRef((props, ref) => (
37+
<span ref={ref}>
38+
<Counter {...props} />
39+
</span>
40+
))
41+
);
42+
HFF = window.__assign(module, 'HFF', HOCFunction(Fwd));
3343

34-
export default Counter;
44+
export default Fwd;

packages/react-scripts/template/src/TODO.md

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,7 @@
2121
- directives?
2222
- when to accept?
2323
- test integrations
24-
25-
lazy()?
26-
27-
- lazy of memo of proxy
28-
29-
- maybe Proxy?
30-
- do I want to preserve type? elementType? or what?
31-
or lazy element?
32-
33-
- what about static fields
34-
35-
- we either break Foo.thing or <Foo />.type?
36-
- maybe we never wrap, but point to latest from current one?
37-
- could also bail out
38-
24+
- exotic (lazy, memo, fwd)
3925
- how to force update all (incl. inside memo)
40-
41-
let A = createA();
42-
B() {
43-
<A />
44-
}
45-
46-
- two kinds of edits
47-
- reeval child with new data
48-
- remember new self
49-
50-
* think in terms of "assign"?
26+
- don't accept module if either assignment failed
27+
- displayName etc

packages/react-scripts/template/src/hot.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export function HotContainer({ children }) {
88
return <HotContext.Provider value={inc}>{children}</HotContext.Provider>;
99
}
1010

11+
let CurrentOwner =
12+
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner;
13+
function readContext(Ctx) {
14+
CurrentOwner.currentDispatcher.readContext(Ctx);
15+
}
16+
1117
let idToPersistentType = new Map();
1218
let idToRawFunction = new Map();
1319
let proxies = new WeakSet();
@@ -24,6 +30,8 @@ function getKind(type) {
2430
return 'memo';
2531
} else if (type.$$typeof === Symbol.for('react.lazy')) {
2632
return 'lazy';
33+
} else if (type.$$typeof === Symbol.for('react.forward_ref')) {
34+
return 'forward_ref';
2735
}
2836
}
2937
return 'other';
@@ -40,7 +48,7 @@ function init(rawType, id) {
4048
const proxy = new Proxy(rawType, {
4149
apply(target, thisArg, args) {
4250
let ret = idToRawFunction.get(id).apply(null, args);
43-
React.useContext(HotContext);
51+
readContext(HotContext);
4452
return ret;
4553
},
4654
});
@@ -54,6 +62,10 @@ function init(rawType, id) {
5462
case 'lazy': {
5563
return rawType;
5664
}
65+
case 'forward_ref': {
66+
rawType.render = init(rawType.render, id);
67+
return rawType;
68+
}
5769
default: {
5870
return rawType;
5971
}
@@ -74,6 +86,9 @@ function accept(type, nextRawType, id) {
7486
case 'memo': {
7587
return accept(type.type, nextRawType.type, id);
7688
}
89+
case 'forward_ref': {
90+
return accept(type.render, nextRawType.render, id);
91+
}
7792
case 'lazy': {
7893
return true;
7994
}

0 commit comments

Comments
 (0)