Commit 1fec2530 authored by Shen Chang's avatar Shen Chang

feat: Fix call stack for useKeepAliveEffect, Fix name error

parent f743aafa
...@@ -43,14 +43,14 @@ function App() { ...@@ -43,14 +43,14 @@ function App() {
<Route <Route
path="/b" path="/b"
render={() => ( render={() => (
<KeepAlive key="B"><B /></KeepAlive> <KeepAlive name="A"><B /></KeepAlive>
)} )}
/> />
<Route <Route
path="/c" path="/c"
render={() => ( render={() => (
<KeepAlive key="C"> <KeepAlive name="B">
<C /> <C />
</KeepAlive> </KeepAlive>
)} )}
......
import React, {useState} from 'react'; import React, {useState, useEffect} from 'react';
import {useKeepAliveEffect} from '../../../es'; import {useKeepAliveEffect} from '../../../es';
function Test() { function Test() {
const [index, setIndex] = useState(0); const [index, setIndex] = useState(0);
useKeepAliveEffect(() => { useKeepAliveEffect(() => {
console.log('activated'); console.log('activated', index);
const i = 0;
return () => { return () => {
console.log('unactivated'); console.log('unactivated', index, i);
}; };
}); });
return ( return (
......
{ {
"name": "react-keep-alive", "name": "react-keep-alive",
"version": "1.2.0", "version": "1.2.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",
......
...@@ -7,10 +7,13 @@ interface IKeepAliveProps { ...@@ -7,10 +7,13 @@ interface IKeepAliveProps {
key?: string; key?: string;
name?: string; name?: string;
disabled?: boolean; disabled?: boolean;
}
interface IKeepAliveInnerProps extends IKeepAliveProps {
_container: any; _container: any;
} }
class KeepAlive extends React.PureComponent<IKeepAliveProps> { class KeepAlive extends React.PureComponent<IKeepAliveInnerProps> {
private bindUnmount: (() => void) | null = null; private bindUnmount: (() => void) | null = null;
public componentDidMount() { public componentDidMount() {
......
...@@ -94,8 +94,10 @@ export default class KeepAliveProvider extends React.PureComponent<IKeepAlivePro ...@@ -94,8 +94,10 @@ export default class KeepAliveProvider extends React.PureComponent<IKeepAlivePro
this.forceUpdate(); this.forceUpdate();
} }
public componentDidCatch() { public componentDidCatch(_: any, info: any) {
warn('[React Keep Alive] Cached components have duplicates. Please check the <KeepAlive> component of the key duplication!'); if (info.componentStack.indexOf(keepAliveProviderTypeName) !== -1) {
warn('[React Keep Alive] Cached components have duplicates. Please check the <KeepAlive> component of the key duplication!');
}
} }
public unactivate = (identification: string) => { public unactivate = (identification: string) => {
......
...@@ -283,6 +283,7 @@ export default function keepAliveDecorator<P = any>(Component: React.ComponentTy ...@@ -283,6 +283,7 @@ export default function keepAliveDecorator<P = any>(Component: React.ComponentTy
? ( ? (
<TriggerLifecycleContainer <TriggerLifecycleContainer
{...wrapperProps} {...wrapperProps}
key={propKey}
propKey={propKey} propKey={propKey}
keepAlive={combinedKeepAlive} keepAlive={combinedKeepAlive}
/> />
......
import React, {useEffect, useContext} from 'react'; import React, {useEffect, useContext, useRef} from 'react';
import {warn} from './debug'; import {warn} from './debug';
import {COMMAND} from './keepAliveDecorator'; import {COMMAND} from './keepAliveDecorator';
import IdentificationContext, {IIdentificationContextProps} from '../contexts/IdentificationContext'; import IdentificationContext, {IIdentificationContextProps} from '../contexts/IdentificationContext';
...@@ -11,15 +11,17 @@ export default function useKeepAliveEffect(effect: React.EffectCallback) { ...@@ -11,15 +11,17 @@ export default function useKeepAliveEffect(effect: React.EffectCallback) {
eventEmitter, eventEmitter,
identification, identification,
} = useContext<IIdentificationContextProps>(IdentificationContext); } = useContext<IIdentificationContextProps>(IdentificationContext);
const effectRef: React.MutableRefObject<React.EffectCallback> = useRef(effect);
effectRef.current = effect;
useEffect(() => { useEffect(() => {
let bindMount: (() => void) | null = null; let bindMount: (() => void) | null = null;
let bindUnmount: (() => void) | null = null; let bindUnmount: (() => void) | null = null;
let effectResult = effect(); let effectResult = effectRef.current();
let unmounted = false; let unmounted = false;
eventEmitter.on( eventEmitter.on(
[identification, COMMAND.MOUNT], [identification, COMMAND.MOUNT],
bindMount = () => { bindMount = () => {
effectResult = effect(); effectResult = effectRef.current();
unmounted = false; unmounted = false;
}, },
true, true,
......
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