Skip to content

Commit af4c459

Browse files
authored
Golf down compat (#5025)
* Make useFlushSync do sync flushes * Golf bytes to offset change * Some more golfing * Revert "Make useFlushSync do sync flushes" This reverts commit 001bbce.
1 parent b4f9cab commit af4c459

File tree

6 files changed

+43
-64
lines changed

6 files changed

+43
-64
lines changed

compat/src/index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function isMemo(element) {
7878
return (
7979
!!element &&
8080
typeof element.displayName == 'string' &&
81-
element.displayName.startsWith('Memo(')
81+
element.displayName.indexOf('Memo(') == 0
8282
);
8383
}
8484

@@ -141,12 +141,6 @@ const unstable_batchedUpdates = (callback, arg) => callback(arg);
141141
*/
142142
const flushSync = (callback, arg) => callback(arg);
143143

144-
/**
145-
* Strict Mode is not implemented in Preact, so we provide a stand-in for it
146-
* that just renders its children without imposing any restrictions.
147-
*/
148-
const StrictMode = Fragment;
149-
150144
// compat to react-is
151145
export const isElement = isValidElement;
152146

@@ -180,7 +174,7 @@ export {
180174
useTransition,
181175
// eslint-disable-next-line camelcase
182176
unstable_batchedUpdates,
183-
StrictMode,
177+
Fragment as StrictMode,
184178
Suspense,
185179
SuspenseList,
186180
lazy,
@@ -228,7 +222,7 @@ export default {
228222
forwardRef,
229223
flushSync,
230224
unstable_batchedUpdates,
231-
StrictMode,
225+
StrictMode: Fragment,
232226
Suspense,
233227
SuspenseList,
234228
lazy,

compat/src/memo.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,21 @@ import { shallowDiffers } from './util';
1111
export function memo(c, comparer) {
1212
function shouldUpdate(nextProps) {
1313
let ref = this.props.ref;
14-
let updateRef = ref == nextProps.ref;
15-
if (!updateRef && ref) {
16-
ref.call ? ref(null) : (ref.current = null);
14+
if (ref != nextProps.ref && ref) {
15+
typeof ref == 'function' ? ref(null) : (ref.current = null);
1716
}
1817

19-
if (!comparer) {
20-
return shallowDiffers(this.props, nextProps);
21-
}
22-
23-
return !comparer(this.props, nextProps) || !updateRef;
18+
return comparer
19+
? !comparer(this.props, nextProps) || ref != nextProps.ref
20+
: shallowDiffers(this.props, nextProps);
2421
}
2522

2623
function Memoed(props) {
2724
this.shouldComponentUpdate = shouldUpdate;
2825
return createElement(c, props);
2926
}
3027
Memoed.displayName = 'Memo(' + (c.displayName || c.name) + ')';
31-
Memoed.prototype.isReactComponent = true;
32-
Memoed._forwarded = true;
28+
Memoed._forwarded = Memoed.prototype.isReactComponent = true;
3329
Memoed.type = c;
3430
return Memoed;
3531
}

compat/src/render.js

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const onChangeInputType = type =>
4545
).test(type);
4646

4747
// Some libraries like `react-virtualized` explicitly check for this.
48-
Component.prototype.isReactComponent = {};
48+
Component.prototype.isReactComponent = true;
4949

5050
// `UNSAFE_*` lifecycle hooks
5151
// Preact only ever invokes the unprefixed methods.
@@ -105,24 +105,17 @@ let oldEventHook = options.event;
105105
options.event = e => {
106106
if (oldEventHook) e = oldEventHook(e);
107107

108-
e.persist = empty;
109-
e.isPropagationStopped = isPropagationStopped;
110-
e.isDefaultPrevented = isDefaultPrevented;
108+
e.persist = () => {};
109+
e.isPropagationStopped = function isPropagationStopped() {
110+
return this.cancelBubble;
111+
};
112+
e.isDefaultPrevented = function isDefaultPrevented() {
113+
return this.defaultPrevented;
114+
};
111115
return (e.nativeEvent = e);
112116
};
113117

114-
function empty() {}
115-
116-
function isPropagationStopped() {
117-
return this.cancelBubble;
118-
}
119-
120-
function isDefaultPrevented() {
121-
return this.defaultPrevented;
122-
}
123-
124118
const classNameDescriptorNonEnumberable = {
125-
enumerable: false,
126119
configurable: true,
127120
get() {
128121
return this.class;
@@ -132,9 +125,9 @@ const classNameDescriptorNonEnumberable = {
132125
function handleDomVNode(vnode) {
133126
let props = vnode.props,
134127
type = vnode.type,
135-
normalizedProps = {};
128+
normalizedProps = {},
129+
isNonDashedType = type.indexOf('-') == -1;
136130

137-
let isNonDashedType = type.indexOf('-') === -1;
138131
for (let i in props) {
139132
let value = props[i];
140133

@@ -198,30 +191,28 @@ function handleDomVNode(vnode) {
198191
normalizedProps[i] = value;
199192
}
200193

201-
// Add support for array select values: <select multiple value={[]} />
202-
if (
203-
type == 'select' &&
204-
normalizedProps.multiple &&
205-
Array.isArray(normalizedProps.value)
206-
) {
207-
// forEach() always returns undefined, which we abuse here to unset the value prop.
208-
normalizedProps.value = toChildArray(props.children).forEach(child => {
209-
child.props.selected =
210-
normalizedProps.value.indexOf(child.props.value) != -1;
211-
});
212-
}
213-
214-
// Adding support for defaultValue in select tag
215-
if (type == 'select' && normalizedProps.defaultValue != null) {
216-
normalizedProps.value = toChildArray(props.children).forEach(child => {
217-
if (normalizedProps.multiple) {
218-
child.props.selected =
219-
normalizedProps.defaultValue.indexOf(child.props.value) != -1;
220-
} else {
194+
if (type == 'select') {
195+
// Add support for array select values: <select multiple value={[]} />
196+
if (normalizedProps.multiple && Array.isArray(normalizedProps.value)) {
197+
// forEach() always returns undefined, which we abuse here to unset the value prop.
198+
normalizedProps.value = toChildArray(props.children).forEach(child => {
221199
child.props.selected =
222-
normalizedProps.defaultValue == child.props.value;
223-
}
224-
});
200+
normalizedProps.value.indexOf(child.props.value) != -1;
201+
});
202+
}
203+
204+
// Adding support for defaultValue in select tag
205+
if (normalizedProps.defaultValue != null) {
206+
normalizedProps.value = toChildArray(props.children).forEach(child => {
207+
if (normalizedProps.multiple) {
208+
child.props.selected =
209+
normalizedProps.defaultValue.indexOf(child.props.value) != -1;
210+
} else {
211+
child.props.selected =
212+
normalizedProps.defaultValue == child.props.value;
213+
}
214+
});
215+
}
225216
}
226217

227218
if (props.class && !props.className) {

compat/src/suspense.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,7 @@ Suspense.prototype.render = function (props, state) {
245245
* @returns {((unsuspend: () => void) => void)?}
246246
*/
247247
export function suspended(vnode) {
248-
if (!vnode._parent) return null;
249-
/** @type {import('./internal').Component} */
250-
let component = vnode._parent._component;
248+
let component = vnode._parent && vnode._parent._component;
251249
return component && component._suspended && component._suspended(vnode);
252250
}
253251

compat/test/browser/PureComponent.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,6 @@ describe('PureComponent', () => {
151151

152152
it('should have "isPureReactComponent" property', () => {
153153
let Pure = new React.PureComponent();
154-
expect(Pure.isReactComponent).to.deep.equal({});
154+
expect(Pure.isReactComponent).to.deep.equal(true);
155155
});
156156
});

compat/test/browser/component.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('components', () => {
2020

2121
it('should have "isReactComponent" property', () => {
2222
let Comp = new React.Component();
23-
expect(Comp.isReactComponent).to.deep.equal({});
23+
expect(Comp.isReactComponent).to.deep.equal(true);
2424
});
2525

2626
it('should be sane', () => {

0 commit comments

Comments
 (0)