Skip to content

Commit

Permalink
fix: OffScreen to Activity
Browse files Browse the repository at this point in the history
  • Loading branch information
luhc228 committed Dec 14, 2023
1 parent ebc8ae0 commit 91ee9dc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
6 changes: 4 additions & 2 deletions examples/with-keep-alive/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# with-keep-alive

Experimental keep-alive with React 18 `<Offscreen />`.
Experimental keep-alive with React 18 `<Activity />`.

## How to debug

Expand All @@ -15,9 +15,11 @@ $ cd packages/runtime && yalc publish --push
Then, install the example dependencies.

```bash
$ cd examples/with-keep-alive && yarn install
$ cd examples/with-keep-alive

$ yalc add @ice/app @ice/runtime

$ yarn install

$ npm run start
```
10 changes: 6 additions & 4 deletions examples/with-keep-alive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
"build": "ice build"
},
"dependencies": {
"@ice/runtime": "alpha",
"react": "experimental",
"react-dom": "experimental"
"react": "0.0.0-experimental-0cdfef19b-20231211",
"react-dom": "0.0.0-experimental-0cdfef19b-20231211"
},
"devDependencies": {
"@ice/app": "alpha",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.2"
},
"resolutions": {
"react": "0.0.0-experimental-0cdfef19b-20231211",
"react-dom": "0.0.0-experimental-0cdfef19b-20231211"
}
}
8 changes: 4 additions & 4 deletions packages/runtime/src/KeepAliveOutlet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import React, { useState, useEffect } from 'react';
import { useOutlet, useLocation } from 'react-router-dom';

// @ts-ignore
const Offscreen = React.unstable_Offscreen;
const Activity = React.unstable_Activity;

// ref: https://leomyili.github.io/react-stillness-component/docs/examples/react-router/v6
export default function KeepAliveOutlet() {
if (!Offscreen) {
if (!Activity) {
throw new Error('`<KeepAliveOutlet />` now requires react experimental version. Please install it first.');
}
const [outlets, setOutlets] = useState([]);
Expand All @@ -32,9 +32,9 @@ export default function KeepAliveOutlet() {
{
outlets.map((o) => {
return (
<Offscreen key={o.key} mode={location.pathname === o.pathname ? 'visible' : 'hidden'}>
<Activity key={o.key} mode={location.pathname === o.pathname ? 'visible' : 'hidden'}>
{o.outlet}
</Offscreen>
</Activity>
);
})
}
Expand Down
12 changes: 6 additions & 6 deletions website/docs/guide/advanced/keep-alive.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export default function Layout() {

## 缓存其他组件

除了缓存路由组件,还可以直接使用 React 18 提供的实验特性 `<Offscreen />` 组件,进一步缓存更细粒度的组件。
除了缓存路由组件,还可以直接使用 React 18 提供的实验特性 `<Activity />` 组件,进一步缓存更细粒度的组件。

```tsx
import React from 'react';

// @ts-ignore
const Offscreen = React.unstable_Offscreen;
const Activity = React.unstable_Activity;

export default function Home() {
const [auth, setAuth] = React.useState('admin');
Expand All @@ -65,12 +65,12 @@ export default function Home() {
<button onClick={() => setAuth('user')}>Set User</button>
</div>
<>
<Offscreen mode={auth === 'admin' ? 'visible' : 'hidden'}>
<Activity mode={auth === 'admin' ? 'visible' : 'hidden'}>
Admin Name: <input />
</Offscreen>
<Offscreen mode={auth === 'user' ? 'visible' : 'hidden'}>
</Activity>
<Activity mode={auth === 'user' ? 'visible' : 'hidden'}>
User Name: <input />
</Offscreen>
</Activity>
</>
</>
)
Expand Down

0 comments on commit 91ee9dc

Please sign in to comment.