Commit 0d0407e0 authored by Shen Chang's avatar Shen Chang

feat(Add bindLifecycle error checking):

parent 3047b0d5
......@@ -66,7 +66,8 @@ class App extends React.Component {
<Route
path="/b"
render={() => (
<B />
<KeepAlive key="B"><B /></KeepAlive>
)}
/>
<Route
......
{
"name": "react-keep-alive",
"version": "0.3.0",
"version": "0.3.1",
"description": "Package will allow components to maintain their status, to avoid repeated re-rendering.",
"author": "Shen Chang",
"homepage": "https://github.com/Sam618/react-keep-alive",
......
......@@ -22,7 +22,7 @@ class Consumer extends React.PureComponent<IConsumerProps> {
super(props, ...args);
const {cache, setCache, children} = props;
if (!cache || !setCache) {
warn('[React Keep Alive] <KeepAlive> component must be in the <Provider> component.');
warn('[React Keep Alive] You should not use <KeepAlive> outside a <Provider>.');
}
React.Children.only(children);
}
......
......@@ -31,4 +31,4 @@ class KeepAlive extends React.PureComponent<IKeepAliveProps> {
}
}
export default keepAlive(KeepAlive) as React.ComponentClass<IKeepAliveProps>;
export default keepAlive<IKeepAliveProps>(KeepAlive);
import React from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';
import noop from './noop';
import {warn} from './debug';
import {COMMAND} from './keepAlive';
import withIdentificationContextConsumer from './withIdentificationContextConsumer';
import getDisplayName from './getDisplayName';
......@@ -94,22 +95,24 @@ export default function bindLifecycle<P = any>(Component: React.ComponentClass<P
keepAlive,
},
...wrapperProps
}) => (
identification
? (
<Component
{...wrapperProps}
keepAlive={keepAlive}
ref={forwardRef || noop}
_container={{
identification,
eventEmitter,
activated,
}}
/>
)
: null
),
}) => {
if (!identification) {
warn('[React Keep Alive] You should not use bindLifecycle outside a <KeepAlive>.');
return null;
}
return (
<Component
{...wrapperProps}
ref={forwardRef || noop}
_container={{
identification,
eventEmitter,
activated,
keepAlive,
}}
/>
);
},
);
const BindLifecycle = React.forwardRef((props: P, ref) => (
<BindLifecycleHOC {...props} forwardRef={ref} />
......
......@@ -39,7 +39,7 @@ interface ITriggerLifecycleContainerProps extends IKeepAliveContextConsumerCompo
getCombinedKeepAlive: () => boolean;
}
export default (Component: React.ComponentType<any>) => {
export default function keepAliveDecorator<P = any>(Component: React.ComponentType<any>): React.ComponentType<P> {
const {
componentDidMount = noop,
componentDidUpdate = noop,
......@@ -50,12 +50,12 @@ export default (Component: React.ComponentType<any>) => {
Component.prototype.componentDidMount = function () {
const {
_container,
keepAlive,
} = this.props;
const {
notNeedActivate,
identification,
eventEmitter,
keepAlive,
} = _container;
notNeedActivate();
const cb = () => {
......@@ -300,7 +300,6 @@ export default (Component: React.ComponentType<any>) => {
>
<Component
{...wrapperProps}
keepAlive={keepAlive}
_container={{
isNeedActivate,
notNeedActivate,
......@@ -308,6 +307,7 @@ export default (Component: React.ComponentType<any>) => {
eventEmitter,
identification,
storeElement,
keepAlive,
cache,
}}
/>
......@@ -449,4 +449,4 @@ export default (Component: React.ComponentType<any>) => {
) as any;
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