Commit 38b4d116 authored by Shen Chang's avatar Shen Chang

fix(with(.*).ts): repair multi-layer decorator packaging to return to the original component

parent 01211338
...@@ -3,7 +3,7 @@ Package will allow components to maintain their status, to avoid repeated re-ren ...@@ -3,7 +3,7 @@ Package will allow components to maintain their status, to avoid repeated re-ren
## TODO ## TODO
- MAX - MAX
- wrappedComponent - wrappedComponent
- document - document
- test - test
......
{ {
"name": "react-keep-alive", "name": "react-keep-alive",
"version": "0.2.0", "version": "0.2.2",
"description": "Keep the state of the component.", "description": "Package will allow components to maintain their status, to avoid repeated re-rendering.",
"author": "Shen Chang", "author": "Shen Chang",
"homepage": "https://github.com/Sam618/react-keep-alive", "homepage": "https://github.com/Sam618/react-keep-alive",
"keywords": [ "keywords": [
......
...@@ -6,13 +6,7 @@ import withIdentificationContextConsumer from './withIdentificationContextConsum ...@@ -6,13 +6,7 @@ import withIdentificationContextConsumer from './withIdentificationContextConsum
import getDisplayName from './getDisplayName'; import getDisplayName from './getDisplayName';
export default function bindLifecycle<P = any>(Component: React.ComponentType<P>) { export default function bindLifecycle<P = any>(Component: React.ComponentType<P>) {
const { const WrappedComponent = (Component as any).WrappedComponent || (Component as any).wrappedComponent || Component;
WrappedComponent,
wrappedComponent,
} = Component as any;
if (WrappedComponent || wrappedComponent) {
Component = WrappedComponent || wrappedComponent;
}
const { const {
componentDidMount = noop, componentDidMount = noop,
...@@ -20,9 +14,9 @@ export default function bindLifecycle<P = any>(Component: React.ComponentType<P> ...@@ -20,9 +14,9 @@ export default function bindLifecycle<P = any>(Component: React.ComponentType<P>
componentDidActivate = noop, componentDidActivate = noop,
componentWillUnactivate = noop, componentWillUnactivate = noop,
componentWillUnmount = noop, componentWillUnmount = noop,
} = Component.prototype; } = WrappedComponent.prototype;
Component.prototype.componentDidMount = function () { WrappedComponent.prototype.componentDidMount = function () {
componentDidMount.call(this); componentDidMount.call(this);
this._needActivate = false; this._needActivate = false;
const { const {
...@@ -59,14 +53,14 @@ export default function bindLifecycle<P = any>(Component: React.ComponentType<P> ...@@ -59,14 +53,14 @@ export default function bindLifecycle<P = any>(Component: React.ComponentType<P>
true, true,
); );
}; };
Component.prototype.componentDidUpdate = function () { WrappedComponent.prototype.componentDidUpdate = function () {
componentDidUpdate.call(this); componentDidUpdate.call(this);
if (this._needActivate) { if (this._needActivate) {
this._needActivate = false; this._needActivate = false;
componentDidActivate.call(this); componentDidActivate.call(this);
} }
}; };
Component.prototype.componentWillUnmount = function () { WrappedComponent.prototype.componentWillUnmount = function () {
if (!this._unmounted) { if (!this._unmounted) {
componentWillUnmount.call(this); componentWillUnmount.call(this);
} }
...@@ -121,7 +115,7 @@ export default function bindLifecycle<P = any>(Component: React.ComponentType<P> ...@@ -121,7 +115,7 @@ export default function bindLifecycle<P = any>(Component: React.ComponentType<P>
<BindLifecycleHOC {...props} forwardRef={ref} /> <BindLifecycleHOC {...props} forwardRef={ref} />
)); ));
(BindLifecycle as any).WrappedComponent = Component; (BindLifecycle as any).WrappedComponent = WrappedComponent;
BindLifecycle.displayName = `bindLifecycle(${getDisplayName(Component)})`; BindLifecycle.displayName = `bindLifecycle(${getDisplayName(Component)})`;
return hoistNonReactStatics( return hoistNonReactStatics(
BindLifecycle, BindLifecycle,
......
...@@ -18,6 +18,8 @@ export enum COMMAND { ...@@ -18,6 +18,8 @@ export enum COMMAND {
UNACTIVATE = 'unactivate', UNACTIVATE = 'unactivate',
UNMOUNT = 'unmount', UNMOUNT = 'unmount',
ACTIVATE = 'activate', ACTIVATE = 'activate',
CURRENT_UNMOUNT = 'current_unmount',
CURRENT_UNACTIVATE = 'current_unactivate',
} }
export interface IOptions { export interface IOptions {
...@@ -56,13 +58,7 @@ export default function keepAliveDecorator({ ...@@ -56,13 +58,7 @@ export default function keepAliveDecorator({
forwardRef = false, forwardRef = false,
}: IOptions = {}) { }: IOptions = {}) {
return (Component: React.ComponentType<IComponentProps>) => { return (Component: React.ComponentType<IComponentProps>) => {
const { const WrappedComponent = (Component as any).WrappedComponent || (Component as any).wrappedComponent || Component;
WrappedComponent,
wrappedComponent,
} = Component as any;
if (WrappedComponent || wrappedComponent) {
Component = WrappedComponent || wrappedComponent;
}
const { const {
componentDidMount = noop, componentDidMount = noop,
...@@ -70,14 +66,14 @@ export default function keepAliveDecorator({ ...@@ -70,14 +66,14 @@ export default function keepAliveDecorator({
componentDidActivate = noop, componentDidActivate = noop,
componentWillUnactivate = noop, componentWillUnactivate = noop,
componentWillUnmount = noop, componentWillUnmount = noop,
} = Component.prototype; } = WrappedComponent.prototype;
const displayName = (name || getDisplayName(Component)) as any; const displayName = (name || getDisplayName(Component)) as any;
if (!displayName) { if (!displayName) {
warn('[React Keep Alive] Each component must have a name, which can be the component\'s displayName or name static property. You can also configure name when keepAlive decorates the component.'); warn('[React Keep Alive] Each component must have a name, which can be the component\'s displayName or name static property. You can also configure name when keepAlive decorates the component.');
} }
Component.prototype.componentDidMount = function () { WrappedComponent.prototype.componentDidMount = function () {
const { const {
_container, _container,
keepAlive, keepAlive,
...@@ -95,10 +91,28 @@ export default function keepAliveDecorator({ ...@@ -95,10 +91,28 @@ export default function keepAliveDecorator({
eventEmitter.on([identification, START_MOUNTING_DOM], cb); eventEmitter.on([identification, START_MOUNTING_DOM], cb);
componentDidMount.call(this); componentDidMount.call(this);
if (keepAlive) { if (keepAlive) {
componentDidActivate.call(this); this.componentDidActivate();
} }
}; };
Component.prototype.componentDidUpdate = function () { WrappedComponent.prototype.componentDidActivate = function () {
const {
_container,
} = this.props;
const {
identification,
eventEmitter,
} = _container;
componentDidActivate.call(this);
eventEmitter.on(
[identification, COMMAND.CURRENT_UNMOUNT],
this._bindUnmount = this.componentWillUnmount.bind(this),
);
eventEmitter.on(
[identification, COMMAND.CURRENT_UNACTIVATE],
this._bindUnactivate = this.componentWillUnactivate.bind(this),
);
};
WrappedComponent.prototype.componentDidUpdate = function () {
componentDidUpdate.call(this); componentDidUpdate.call(this);
const { const {
_container, _container,
...@@ -111,14 +125,14 @@ export default function keepAliveDecorator({ ...@@ -111,14 +125,14 @@ export default function keepAliveDecorator({
notNeedActivate(); notNeedActivate();
mount.call(this); mount.call(this);
this._unmounted = false; this._unmounted = false;
componentDidActivate.call(this); this.componentDidActivate();
} }
}; };
Component.prototype.componentWillUnactivate = function () { WrappedComponent.prototype.componentWillUnactivate = function () {
componentWillUnactivate.call(this); componentWillUnactivate.call(this);
unmount.call(this); unmount.call(this);
}; };
Component.prototype.componentWillUnmount = function () { WrappedComponent.prototype.componentWillUnmount = function () {
// Because we will manually call the componentWillUnmount lifecycle // Because we will manually call the componentWillUnmount lifecycle
// so we need to prevent it from firing multiple times // so we need to prevent it from firing multiple times
if (!this._unmounted) { if (!this._unmounted) {
...@@ -149,9 +163,12 @@ export default function keepAliveDecorator({ ...@@ -149,9 +163,12 @@ export default function keepAliveDecorator({
storeElement, storeElement,
cache, cache,
setLifecycle, setLifecycle,
eventEmitter,
}, },
} = this.props; } = this.props;
const {renderElement, ifStillActivate, reactivate} = cache[identification]; const {renderElement, ifStillActivate, reactivate} = cache[identification];
eventEmitter.off([identification, COMMAND.CURRENT_UNMOUNT], this._bindUnmount);
eventEmitter.off([identification, COMMAND.CURRENT_UNACTIVATE], this._bindUnactivate);
setLifecycle(LIFECYCLE.UNMOUNTED); setLifecycle(LIFECYCLE.UNMOUNTED);
changePositionByComment(identification, storeElement, renderElement); changePositionByComment(identification, storeElement, renderElement);
if (ifStillActivate) { if (ifStillActivate) {
...@@ -162,8 +179,6 @@ export default function keepAliveDecorator({ ...@@ -162,8 +179,6 @@ export default function keepAliveDecorator({
class TriggerLifecycleContainer extends React.PureComponent<ITriggerLifecycleContainerProps> { class TriggerLifecycleContainer extends React.PureComponent<ITriggerLifecycleContainerProps> {
private identification: string; private identification: string;
private ref: any;
private activated = false; private activated = false;
private ifStillActivate = false; private ifStillActivate = false;
...@@ -205,17 +220,13 @@ export default function keepAliveDecorator({ ...@@ -205,17 +220,13 @@ export default function keepAliveDecorator({
} = this.props; } = this.props;
const keepAlive = getCombinedKeepAlive(); const keepAlive = getCombinedKeepAlive();
if (!keepAlive || !isExisted()) { if (!keepAlive || !isExisted()) {
if (this.ref) { eventEmitter.emit([this.identification, COMMAND.CURRENT_UNMOUNT]);
this.ref.componentWillUnmount();
}
eventEmitter.emit([this.identification, COMMAND.UNMOUNT]); eventEmitter.emit([this.identification, COMMAND.UNMOUNT]);
} }
// When the Provider components are unmounted, the cache is not needed, // When the Provider components are unmounted, the cache is not needed,
// so you don't have to execute the componentWillUnactivate lifecycle. // so you don't have to execute the componentWillUnactivate lifecycle.
if (keepAlive && isExisted()) { if (keepAlive && isExisted()) {
if (this.ref) { eventEmitter.emit([this.identification, COMMAND.CURRENT_UNACTIVATE]);
this.ref.componentWillUnactivate();
}
eventEmitter.emit([this.identification, COMMAND.UNACTIVATE]); eventEmitter.emit([this.identification, COMMAND.UNACTIVATE]);
} }
} }
...@@ -245,16 +256,6 @@ export default function keepAliveDecorator({ ...@@ -245,16 +256,6 @@ export default function keepAliveDecorator({
this.lifecycle = lifecycle; this.lifecycle = lifecycle;
} }
private setRef = (ref: any) => {
this.ref = ref;
const {
forwardedRef,
} = this.props;
if (forwardedRef) {
(forwardedRef as any)(ref);
}
}
public render() { public render() {
if (!this.identification) { if (!this.identification) {
// We need to generate a corresponding unique identifier based on the information of the component. // We need to generate a corresponding unique identifier based on the information of the component.
...@@ -284,7 +285,6 @@ export default function keepAliveDecorator({ ...@@ -284,7 +285,6 @@ export default function keepAliveDecorator({
activated, activated,
getLifecycle, getLifecycle,
setLifecycle, setLifecycle,
setRef,
identification, identification,
ifStillActivate, ifStillActivate,
} = this; } = this;
...@@ -324,7 +324,7 @@ export default function keepAliveDecorator({ ...@@ -324,7 +324,7 @@ export default function keepAliveDecorator({
<Component <Component
{...wrapperProps} {...wrapperProps}
keepAlive={keepAlive} keepAlive={keepAlive}
ref={setRef} ref={forwardedRef as React.Ref<{}>}
_container={{ _container={{
isNeedActivate, isNeedActivate,
notNeedActivate, notNeedActivate,
...@@ -476,7 +476,7 @@ export default function keepAliveDecorator({ ...@@ -476,7 +476,7 @@ export default function keepAliveDecorator({
)); ));
} }
(KeepAlive as any).WrappedComponent = Component; (KeepAlive as any).WrappedComponent = WrappedComponent;
KeepAlive.displayName = `${keepAliveDisplayName}(${displayName})`; KeepAlive.displayName = `${keepAliveDisplayName}(${displayName})`;
return hoistNonReactStatics(KeepAlive, Component); return hoistNonReactStatics(KeepAlive, Component);
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment