Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
react-keep-alive
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
react-keep-alive
Commits
3e12c088
Commit
3e12c088
authored
Mar 07, 2019
by
Shen Chang
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'fix'
parents
a9990e2b
0a6f08ca
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
33 deletions
+48
-33
demo/src/views/B.js
demo/src/views/B.js
+5
-1
demo/src/views/C.js
demo/src/views/C.js
+5
-2
src/utils/keepAlive.tsx
src/utils/keepAlive.tsx
+38
-30
No files found.
demo/src/views/B.js
View file @
3e12c088
import
React
from
'
react
'
;
import
{
keepAlive
,
bindLifecycle
}
from
'
../../../es
'
;
export
default
class
Test
extends
React
.
Component
{
@
keepAlive
()
class
B
extends
React
.
Component
{
componentWillMount
()
{
console
.
log
(
'
B componentWillMount
'
);
}
...
...
@@ -36,3 +38,5 @@ export default class Test extends React.Component {
);
}
}
export
default
B
;
demo/src/views/C.js
View file @
3e12c088
import
React
from
'
react
'
;
import
{
keepAlive
}
from
'
../../../es
'
;
export
default
class
Test
extends
React
.
Component
{
class
C
extends
React
.
Component
{
componentWillMount
()
{
console
.
log
(
'
C componentWillMount
'
);
}
...
...
@@ -35,4 +36,6 @@ export default class Test extends React.Component {
<
div
>
This
is
c
.
<
/div
>
);
}
}
\ No newline at end of file
}
export
default
keepAlive
()(
C
);
src/utils/keepAlive.tsx
View file @
3e12c088
...
...
@@ -58,22 +58,19 @@ export default function keepAliveDecorator({
forwardRef
=
false
,
}:
IOptions
=
{})
{
return
(
Component
:
React
.
ComponentType
<
IComponentProps
>
)
=>
{
const
WrappedComponent
=
(
Component
as
any
).
WrappedComponent
||
(
Component
as
any
).
wrappedComponent
||
Component
;
const
{
componentDidMount
=
noop
,
componentDidUpdate
=
noop
,
componentDidActivate
=
noop
,
componentWillUnactivate
=
noop
,
componentWillUnmount
=
noop
,
}
=
Wrapped
Component
.
prototype
;
}
=
Component
.
prototype
;
const
displayName
=
(
name
||
getDisplayName
(
Component
))
as
any
;
if
(
!
displayName
)
{
warn
(
'
[React Keep Alive] Each component must have a name, which can be the component
\'
s displayName or name static property. You can also configure name when keepAlive decorates the component.
'
);
}
Wrapped
Component
.
prototype
.
componentDidMount
=
function
()
{
Component
.
prototype
.
componentDidMount
=
function
()
{
const
{
_container
,
keepAlive
,
...
...
@@ -86,6 +83,7 @@ export default function keepAliveDecorator({
notNeedActivate
();
const
cb
=
()
=>
{
mount
.
call
(
this
);
listen
.
call
(
this
);
eventEmitter
.
off
([
identification
,
START_MOUNTING_DOM
],
cb
);
};
eventEmitter
.
on
([
identification
,
START_MOUNTING_DOM
],
cb
);
...
...
@@ -94,25 +92,7 @@ export default function keepAliveDecorator({
this
.
componentDidActivate
();
}
};
WrappedComponent
.
prototype
.
componentDidActivate
=
function
()
{
const
{
_container
,
}
=
this
.
props
;
const
{
identification
,
eventEmitter
,
}
=
_container
;
componentDidActivate
.
call
(
this
);
eventEmitter
.
on
(
[
identification
,
COMMAND
.
CURRENT_UNMOUNT
],
this
.
_bindUnmount
=
this
.
componentWillUnmount
.
bind
(
this
),
);
eventEmitter
.
on
(
[
identification
,
COMMAND
.
CURRENT_UNACTIVATE
],
this
.
_bindUnactivate
=
this
.
componentWillUnactivate
.
bind
(
this
),
);
};
WrappedComponent
.
prototype
.
componentDidUpdate
=
function
()
{
Component
.
prototype
.
componentDidUpdate
=
function
()
{
componentDidUpdate
.
call
(
this
);
const
{
_container
,
...
...
@@ -124,21 +104,24 @@ export default function keepAliveDecorator({
if
(
isNeedActivate
())
{
notNeedActivate
();
mount
.
call
(
this
);
listen
.
call
(
this
);
this
.
_unmounted
=
false
;
this
.
componentDidActivate
();
}
};
Wrapped
Component
.
prototype
.
componentWillUnactivate
=
function
()
{
Component
.
prototype
.
componentWillUnactivate
=
function
()
{
componentWillUnactivate
.
call
(
this
);
unmount
.
call
(
this
);
unlisten
.
call
(
this
);
};
Wrapped
Component
.
prototype
.
componentWillUnmount
=
function
()
{
Component
.
prototype
.
componentWillUnmount
=
function
()
{
// Because we will manually call the componentWillUnmount lifecycle
// so we need to prevent it from firing multiple times
if
(
!
this
.
_unmounted
)
{
this
.
_unmounted
=
true
;
componentWillUnmount
.
call
(
this
);
unmount
.
call
(
this
);
unlisten
.
call
(
this
);
}
};
...
...
@@ -163,12 +146,9 @@ export default function keepAliveDecorator({
storeElement
,
cache
,
setLifecycle
,
eventEmitter
,
},
}
=
this
.
props
;
const
{
renderElement
,
ifStillActivate
,
reactivate
}
=
cache
[
identification
];
eventEmitter
.
off
([
identification
,
COMMAND
.
CURRENT_UNMOUNT
],
this
.
_bindUnmount
);
eventEmitter
.
off
([
identification
,
COMMAND
.
CURRENT_UNACTIVATE
],
this
.
_bindUnactivate
);
setLifecycle
(
LIFECYCLE
.
UNMOUNTED
);
changePositionByComment
(
identification
,
storeElement
,
renderElement
);
if
(
ifStillActivate
)
{
...
...
@@ -176,6 +156,34 @@ export default function keepAliveDecorator({
}
}
function
listen
(
this
:
any
)
{
const
{
_container
:
{
identification
,
eventEmitter
,
},
}
=
this
.
props
;
eventEmitter
.
on
(
[
identification
,
COMMAND
.
CURRENT_UNMOUNT
],
this
.
_bindUnmount
=
this
.
componentWillUnmount
.
bind
(
this
),
);
eventEmitter
.
on
(
[
identification
,
COMMAND
.
CURRENT_UNACTIVATE
],
this
.
_bindUnactivate
=
this
.
componentWillUnactivate
.
bind
(
this
),
);
}
function
unlisten
(
this
:
any
)
{
const
{
_container
:
{
identification
,
eventEmitter
,
},
}
=
this
.
props
;
eventEmitter
.
off
([
identification
,
COMMAND
.
CURRENT_UNMOUNT
],
this
.
_bindUnmount
);
eventEmitter
.
off
([
identification
,
COMMAND
.
CURRENT_UNACTIVATE
],
this
.
_bindUnactivate
);
}
class
TriggerLifecycleContainer
extends
React
.
PureComponent
<
ITriggerLifecycleContainerProps
>
{
private
identification
:
string
;
...
...
@@ -476,7 +484,7 @@ export default function keepAliveDecorator({
));
}
(
KeepAlive
as
any
).
WrappedComponent
=
Wrapped
Component
;
(
KeepAlive
as
any
).
WrappedComponent
=
Component
;
KeepAlive
.
displayName
=
`
${
keepAliveDisplayName
}
(
${
displayName
}
)`
;
return
hoistNonReactStatics
(
KeepAlive
,
Component
);
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment