Commit 41c86f1a authored by Shen Chang's avatar Shen Chang

refactor(Consumer): Not getting DOM through FiberNode, Increase the name prop

parent 18ee48c0
...@@ -35,7 +35,7 @@ function App() { ...@@ -35,7 +35,7 @@ function App() {
<Route <Route
path="/a" path="/a"
render={() => ( render={() => (
<KeepAlive key="A" disabled={!toggle}> <KeepAlive name="Test" disabled={!toggle}>
<A /> <A />
</KeepAlive> </KeepAlive>
)} )}
......
{ {
"name": "react-keep-alive", "name": "react-keep-alive",
"version": "1.0.0", "version": "1.2.0",
"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",
......
import React from 'react'; import React from 'react';
import Comment from './Comment'; import Comment from './Comment';
import {LIFECYCLE, ICache, ICacheItem} from './Provider'; import {LIFECYCLE, ICache, ICacheItem} from './Provider';
import findDOMNodeByFiberNode from '../utils/findDOMNodeByFiberNode';
interface IConsumerProps { interface IConsumerProps {
children: React.ReactNode; children: React.ReactNode;
...@@ -15,6 +14,8 @@ interface IConsumerProps { ...@@ -15,6 +14,8 @@ interface IConsumerProps {
class Consumer extends React.PureComponent<IConsumerProps> { class Consumer extends React.PureComponent<IConsumerProps> {
private renderElement: HTMLElement; private renderElement: HTMLElement;
private commentRef: any;
private identification: string = this.props.identification; private identification: string = this.props.identification;
public componentDidMount() { public componentDidMount() {
...@@ -23,8 +24,7 @@ class Consumer extends React.PureComponent<IConsumerProps> { ...@@ -23,8 +24,7 @@ class Consumer extends React.PureComponent<IConsumerProps> {
children, children,
keepAlive, keepAlive,
} = this.props; } = this.props;
const {_reactInternalFiber} = this as any; this.renderElement = this.commentRef.parentNode;
this.renderElement = findDOMNodeByFiberNode(_reactInternalFiber) as HTMLElement;
setCache(this.identification, { setCache(this.identification, {
children, children,
keepAlive, keepAlive,
...@@ -54,7 +54,7 @@ class Consumer extends React.PureComponent<IConsumerProps> { ...@@ -54,7 +54,7 @@ class Consumer extends React.PureComponent<IConsumerProps> {
public render() { public render() {
const {identification} = this; const {identification} = this;
return <Comment>{identification}</Comment>; return <Comment ref={ref => this.commentRef = ref}>{identification}</Comment>;
} }
} }
......
...@@ -4,7 +4,8 @@ import keepAlive, {COMMAND} from '../utils/keepAliveDecorator'; ...@@ -4,7 +4,8 @@ import keepAlive, {COMMAND} from '../utils/keepAliveDecorator';
import changePositionByComment from '../utils/changePositionByComment'; import changePositionByComment from '../utils/changePositionByComment';
interface IKeepAliveProps { interface IKeepAliveProps {
key: string; key?: string;
name?: string;
disabled?: boolean; disabled?: boolean;
_container: any; _container: any;
} }
......
export default function findDOMNodeByFiberNode(fiberNode: any): HTMLElement | null {
if (!fiberNode) {
return null;
}
const {
stateNode,
return: parent,
} = fiberNode;
if (!parent) {
return stateNode && stateNode.containerInfo;
}
if (stateNode && stateNode.nodeType) {
return stateNode;
}
return findDOMNodeByFiberNode(parent);
}
...@@ -19,6 +19,7 @@ export enum COMMAND { ...@@ -19,6 +19,7 @@ export enum COMMAND {
interface IListenUpperKeepAliveContainerProps extends IIdentificationContextConsumerComponentProps, IKeepAliveContextConsumerComponentProps { interface IListenUpperKeepAliveContainerProps extends IIdentificationContextConsumerComponentProps, IKeepAliveContextConsumerComponentProps {
disabled?: boolean; disabled?: boolean;
name?: string;
} }
interface IListenUpperKeepAliveContainerState { interface IListenUpperKeepAliveContainerState {
...@@ -255,6 +256,7 @@ export default function keepAliveDecorator<P = any>(Component: React.ComponentTy ...@@ -255,6 +256,7 @@ export default function keepAliveDecorator<P = any>(Component: React.ComponentTy
getLifecycle, getLifecycle,
}, },
disabled, disabled,
name,
...wrapperProps ...wrapperProps
} = this.props; } = this.props;
const {activated} = this.state; const {activated} = this.state;
...@@ -266,9 +268,9 @@ export default function keepAliveDecorator<P = any>(Component: React.ComponentTy ...@@ -266,9 +268,9 @@ export default function keepAliveDecorator<P = any>(Component: React.ComponentTy
} = wrapperProps; } = wrapperProps;
// When the parent KeepAlive component is mounted or unmounted, // When the parent KeepAlive component is mounted or unmounted,
// use the keepAlive prop of the parent KeepAlive component. // use the keepAlive prop of the parent KeepAlive component.
const propKey = getKeyByFiberNode((this as any)._reactInternalFiber); const propKey = name || getKeyByFiberNode((this as any)._reactInternalFiber);
if (!propKey) { if (!propKey) {
warn('[React Keep Alive] <KeepAlive> components must have key.'); warn('[React Keep Alive] <KeepAlive> components must have key or name.');
return null; return null;
} }
const newKeepAlive = getKeepAlive(propKey, include, exclude, disabled); const newKeepAlive = getKeepAlive(propKey, include, exclude, disabled);
......
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