Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 003a368

Browse files
siapdevquisido
authored andcommitted
Converts componentWillMount with UNSAFE_componentWillMount (#146)
* First Attempt Convert WillUpdate * Improved decoration with tests for react version * Improved style * Fix Parsing
1 parent 0b27c24 commit 003a368

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/decorator.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ComponentClass } from 'react';
1+
import { ComponentClass, version } from 'react';
22
import { Reducers, State } from '../default';
33
import Callback from '../types/callback';
44
import { ReactNComponentClass } from '../types/component-class';
@@ -58,8 +58,21 @@ export default function ReactN<
5858
}
5959
}
6060

61+
public UNSAFE_componentWillUpdate(...args: [ P, S, any ]): void {
62+
const [ rVerMaj, rVerMin ] = version.split('.').map((v): number => parseInt(v));
63+
if (rVerMaj > 16 || (rVerMaj === 16 && rVerMin >= 3)) {
64+
ReactNComponentWillUpdate(this);
65+
}
66+
if (super.UNSAFE_componentWillUpdate) {
67+
super.UNSAFE_componentWillUpdate(...args);
68+
}
69+
}
70+
6171
public componentWillUpdate(...args: [ P, S, any ]): void {
62-
ReactNComponentWillUpdate(this);
72+
const [ rVerMaj, rVerMin ] = version.split('.').map((v): number => parseInt(v));
73+
if (rVerMaj < 16 || (rVerMaj === 16 && rVerMin < 3)) {
74+
ReactNComponentWillUpdate(this);
75+
}
6376
if (super.componentWillUpdate) {
6477
super.componentWillUpdate(...args);
6578
}

src/utils/bind-lifecycle-methods.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Reducers, State } from '../../default';
22
import { ReactNComponent, ReactNPureComponent } from '../../types/component';
3+
import React = require('react');
34
import {
45
ReactNComponentWillUnmount,
56
ReactNComponentWillUpdate,
@@ -41,11 +42,17 @@ export default function bindLifecycleMethods<
4142
// !componentWillUpdateInstance(that) &&
4243
!componentWillUpdatePrototype(that)
4344
) {
44-
45+
const [ rVerMaj, rVerMin ] = React.version.split('.').map((v): number => parseInt(v));
46+
if (rVerMaj > 16 || (rVerMaj === 16 && rVerMin >= 3)) {
47+
that.UNSAFE_componentWillUpdate = (): void => {
48+
ReactNComponentWillUpdate(that);
49+
};
50+
} else {
51+
that.componentWillUpdate = (): void => {
52+
ReactNComponentWillUpdate(that);
53+
};
54+
}
4555
// Warning: If componentWillUpdate is defined in the constructor (or as an
4656
// arrow function), this will be overridden.
47-
that.componentWillUpdate = (): void => {
48-
ReactNComponentWillUpdate(that);
49-
};
5057
}
5158
};

src/utils/component-will-update.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ export const componentWillUpdatePrototype = <
4646
): boolean => {
4747
const proto: ReactNComponent | ReactNPureComponent =
4848
Object.getPrototypeOf(that);
49+
if (Object.prototype.hasOwnProperty.call(proto, 'UNSAFE_componentWillUpdate')) {
50+
that.UNSAFE_componentWillUpdate = (...args: [ P, S, any ]): void => {
51+
ReactNComponentWillUpdate(that);
52+
proto.UNSAFE_componentWillUpdate.bind(that)(...args);
53+
};
54+
return true;
55+
}
4956
if (Object.prototype.hasOwnProperty.call(proto, 'componentWillUpdate')) {
5057
that.componentWillUpdate = (...args: [ P, S, any ]): void => {
5158
ReactNComponentWillUpdate(that);

0 commit comments

Comments
 (0)