Commit d19f64d0 authored by Shen Chang's avatar Shen Chang

Merge branch 'master' of github.com:Sam618/react-keep-alive

parents 569c79d8 a51bc3cf
# React Keep Alive # React Keep Alive
Package will allow components to maintain their status, to avoid repeated re-rendering.
Writing. ## TODO
- MAX
- wrappedComponent
- document
- test
## Installation
React Keep Alive requires React 16.3 or later.
To use React Keep Alive with your React app:
```bash
npm install --save react-keep-alive
```
## Changelog
Changes are tracked in the [CHANGELOG.md](https://github.com/Sam618/react-keep-alive/blob/master/CHANGELOG.md).
## License
React Keep Alive is available under the [MIT](https://github.com/Sam618/react-keep-alive/blob/master/LICENSE) License.
...@@ -6,6 +6,14 @@ import withIdentificationContextConsumer from './withIdentificationContextConsum ...@@ -6,6 +6,14 @@ 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 {
WrappedComponent,
wrappedComponent,
} = Component;
if (WrappedComponent || wrappedComponent) {
Component = WrappedComponent || wrappedComponent;
}
const { const {
componentDidMount = noop, componentDidMount = noop,
componentDidUpdate = noop, componentDidUpdate = noop,
...@@ -82,7 +90,7 @@ export default function bindLifecycle<P = any>(Component: React.ComponentType<P> ...@@ -82,7 +90,7 @@ export default function bindLifecycle<P = any>(Component: React.ComponentType<P>
); );
}; };
const NewComponent = withIdentificationContextConsumer( const BindLifecycleHOC = withIdentificationContextConsumer(
({ ({
forwardRef, forwardRef,
_identificationContextProps: { _identificationContextProps: {
...@@ -109,12 +117,14 @@ export default function bindLifecycle<P = any>(Component: React.ComponentType<P> ...@@ -109,12 +117,14 @@ export default function bindLifecycle<P = any>(Component: React.ComponentType<P>
: null : null
), ),
); );
const BindLifecycle = React.forwardRef((props, ref) => (
<BindLifecycleHOC {...props} forwardRef={ref} />
));
NewComponent.displayName = `bindLifecycle(${getDisplayName(Component)})`; BindLifecycle.WrappedComponent = Component;
BindLifecycle.displayName = `bindLifecycle(${getDisplayName(Component)})`;
return hoistNonReactStatics( return hoistNonReactStatics(
React.forwardRef((props, ref) => ( BindLifecycle,
<NewComponent {...props} forwardRef={ref} />
)),
Component, Component,
); );
} }
...@@ -56,6 +56,14 @@ export default function keepAliveDecorator({ ...@@ -56,6 +56,14 @@ export default function keepAliveDecorator({
forwardRef = false, forwardRef = false,
}: IOptions = {}) { }: IOptions = {}) {
return (Component: React.ComponentType<IComponentProps>) => { return (Component: React.ComponentType<IComponentProps>) => {
const {
WrappedComponent,
wrappedComponent,
} = Component;
if (WrappedComponent || wrappedComponent) {
Component = WrappedComponent || wrappedComponent;
}
const { const {
componentDidMount = noop, componentDidMount = noop,
componentDidUpdate = noop, componentDidUpdate = noop,
...@@ -335,8 +343,6 @@ export default function keepAliveDecorator({ ...@@ -335,8 +343,6 @@ export default function keepAliveDecorator({
} }
class ListenUpperKeepAliveContainer extends React.Component<IListenUpperKeepAliveContainerProps, IListenUpperKeepAliveContainerState> { class ListenUpperKeepAliveContainer extends React.Component<IListenUpperKeepAliveContainerProps, IListenUpperKeepAliveContainerState> {
public static displayName = `${keepAliveDisplayName}(${displayName})`;
private combinedKeepAlive: boolean; private combinedKeepAlive: boolean;
public state = { public state = {
...@@ -460,15 +466,18 @@ export default function keepAliveDecorator({ ...@@ -460,15 +466,18 @@ export default function keepAliveDecorator({
withIdentificationContextConsumer(ListenUpperKeepAliveContainer) withIdentificationContextConsumer(ListenUpperKeepAliveContainer)
); );
let NewComponent: React.ComponentType<IKeepAliveDecorativeComponentProps> = ListenUpperKeepAliveContainerHOC; let KeepAlive: React.ComponentType<IKeepAliveDecorativeComponentProps> = ListenUpperKeepAliveContainerHOC;
if (forwardRef) { if (forwardRef) {
NewComponent = React.forwardRef((props, ref) => ( KeepAlive = React.forwardRef((props, ref) => (
<ListenUpperKeepAliveContainerHOC <ListenUpperKeepAliveContainerHOC
{...props} {...props}
forwardedRef={ref} forwardedRef={ref}
/> />
)); ));
} }
return hoistNonReactStatics(NewComponent, Component);
KeepAlive.WrappedComponent = Component;
KeepAlive.displayName = `${keepAliveDisplayName}(${displayName})`;
return hoistNonReactStatics(KeepAlive, Component);
}; };
} }
...@@ -18,7 +18,7 @@ export interface IIdentificationContextComponentProps { ...@@ -18,7 +18,7 @@ export interface IIdentificationContextComponentProps {
export default function withIdentificationContextConsumer<P = any>(Component: React.ComponentType<IIdentificationContextComponentProps & P>) { export default function withIdentificationContextConsumer<P = any>(Component: React.ComponentType<IIdentificationContextComponentProps & P>) {
const NewComponent = (props: P) => ( const NewComponent = (props: P) => (
<IdentificationContext.Consumer> <IdentificationContext.Consumer>
{(contextProps: IIdentificationContextProps) => <Component _identificationContextProps={contextProps || {}} {...props} />} {(contextProps: IIdentificationContextProps) => <Component _identificationContextProps={contextProps} {...props} />}
</IdentificationContext.Consumer> </IdentificationContext.Consumer>
); );
......
...@@ -12,7 +12,7 @@ export interface IKeepAliveContextComponentProps { ...@@ -12,7 +12,7 @@ export interface IKeepAliveContextComponentProps {
export default function withKeepAliveContextConsumer<P = any>(Component: React.ComponentType<IKeepAliveContextComponentProps & P>) { export default function withKeepAliveContextConsumer<P = any>(Component: React.ComponentType<IKeepAliveContextComponentProps & P>) {
const NewComponent = (props: P) => ( const NewComponent = (props: P) => (
<KeepAliveContext.Consumer> <KeepAliveContext.Consumer>
{(contextProps: IKeepAliveContextProps) => <Component _keepAliveContextProps={contextProps || {}} {...props} />} {(contextProps: IKeepAliveContextProps) => <Component _keepAliveContextProps={contextProps} {...props} />}
</KeepAliveContext.Consumer> </KeepAliveContext.Consumer>
); );
......
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