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
d19f64d0
Commit
d19f64d0
authored
Mar 05, 2019
by
Shen Chang
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of github.com:Sam618/react-keep-alive
parents
569c79d8
a51bc3cf
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
53 additions
and
13 deletions
+53
-13
README.md
README.md
+22
-1
src/utils/bindLifecycle.tsx
src/utils/bindLifecycle.tsx
+15
-5
src/utils/keepAlive.tsx
src/utils/keepAlive.tsx
+14
-5
src/utils/withIdentificationContextConsumer.tsx
src/utils/withIdentificationContextConsumer.tsx
+1
-1
src/utils/withKeepAliveContextConsumer.tsx
src/utils/withKeepAliveContextConsumer.tsx
+1
-1
No files found.
README.md
View file @
d19f64d0
# 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.
src/utils/bindLifecycle.tsx
View file @
d19f64d0
...
@@ -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
,
);
);
}
}
src/utils/keepAlive.tsx
View file @
d19f64d0
...
@@ -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
);
};
};
}
}
src/utils/withIdentificationContextConsumer.tsx
View file @
d19f64d0
...
@@ -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
>
);
);
...
...
src/utils/withKeepAliveContextConsumer.tsx
View file @
d19f64d0
...
@@ -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
>
);
);
...
...
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