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