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 { ...@@ -66,7 +66,8 @@ class App extends React.Component {
<Route <Route
path="/b" path="/b"
render={() => ( render={() => (
<B /> <KeepAlive key="B"><B /></KeepAlive>
)} )}
/> />
<Route <Route
......
{ {
"name": "react-keep-alive", "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.", "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",
......
...@@ -22,7 +22,7 @@ class Consumer extends React.PureComponent<IConsumerProps> { ...@@ -22,7 +22,7 @@ class Consumer extends React.PureComponent<IConsumerProps> {
super(props, ...args); super(props, ...args);
const {cache, setCache, children} = props; const {cache, setCache, children} = props;
if (!cache || !setCache) { 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); React.Children.only(children);
} }
......
...@@ -31,4 +31,4 @@ class KeepAlive extends React.PureComponent<IKeepAliveProps> { ...@@ -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 React from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics'; import hoistNonReactStatics from 'hoist-non-react-statics';
import noop from './noop'; import noop from './noop';
import {warn} from './debug';
import {COMMAND} from './keepAlive'; import {COMMAND} from './keepAlive';
import withIdentificationContextConsumer from './withIdentificationContextConsumer'; import withIdentificationContextConsumer from './withIdentificationContextConsumer';
import getDisplayName from './getDisplayName'; import getDisplayName from './getDisplayName';
...@@ -94,22 +95,24 @@ export default function bindLifecycle<P = any>(Component: React.ComponentClass<P ...@@ -94,22 +95,24 @@ export default function bindLifecycle<P = any>(Component: React.ComponentClass<P
keepAlive, keepAlive,
}, },
...wrapperProps ...wrapperProps
}) => ( }) => {
identification if (!identification) {
? ( warn('[React Keep Alive] You should not use bindLifecycle outside a <KeepAlive>.');
return null;
}
return (
<Component <Component
{...wrapperProps} {...wrapperProps}
keepAlive={keepAlive}
ref={forwardRef || noop} ref={forwardRef || noop}
_container={{ _container={{
identification, identification,
eventEmitter, eventEmitter,
activated, activated,
keepAlive,
}} }}
/> />
) );
: null },
),
); );
const BindLifecycle = React.forwardRef((props: P, ref) => ( const BindLifecycle = React.forwardRef((props: P, ref) => (
<BindLifecycleHOC {...props} forwardRef={ref} /> <BindLifecycleHOC {...props} forwardRef={ref} />
......
...@@ -39,7 +39,7 @@ interface ITriggerLifecycleContainerProps extends IKeepAliveContextConsumerCompo ...@@ -39,7 +39,7 @@ interface ITriggerLifecycleContainerProps extends IKeepAliveContextConsumerCompo
getCombinedKeepAlive: () => boolean; getCombinedKeepAlive: () => boolean;
} }
export default (Component: React.ComponentType<any>) => { export default function keepAliveDecorator<P = any>(Component: React.ComponentType<any>): React.ComponentType<P> {
const { const {
componentDidMount = noop, componentDidMount = noop,
componentDidUpdate = noop, componentDidUpdate = noop,
...@@ -50,12 +50,12 @@ export default (Component: React.ComponentType<any>) => { ...@@ -50,12 +50,12 @@ export default (Component: React.ComponentType<any>) => {
Component.prototype.componentDidMount = function () { Component.prototype.componentDidMount = function () {
const { const {
_container, _container,
keepAlive,
} = this.props; } = this.props;
const { const {
notNeedActivate, notNeedActivate,
identification, identification,
eventEmitter, eventEmitter,
keepAlive,
} = _container; } = _container;
notNeedActivate(); notNeedActivate();
const cb = () => { const cb = () => {
...@@ -300,7 +300,6 @@ export default (Component: React.ComponentType<any>) => { ...@@ -300,7 +300,6 @@ export default (Component: React.ComponentType<any>) => {
> >
<Component <Component
{...wrapperProps} {...wrapperProps}
keepAlive={keepAlive}
_container={{ _container={{
isNeedActivate, isNeedActivate,
notNeedActivate, notNeedActivate,
...@@ -308,6 +307,7 @@ export default (Component: React.ComponentType<any>) => { ...@@ -308,6 +307,7 @@ export default (Component: React.ComponentType<any>) => {
eventEmitter, eventEmitter,
identification, identification,
storeElement, storeElement,
keepAlive,
cache, cache,
}} }}
/> />
...@@ -449,4 +449,4 @@ export default (Component: React.ComponentType<any>) => { ...@@ -449,4 +449,4 @@ export default (Component: React.ComponentType<any>) => {
) as any; ) as any;
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