Skip to content

Commit 79740da

Browse files
authored
Update time-slicing demo (#21401)
1 parent b6644fa commit 79740da

File tree

5 files changed

+3509
-1822
lines changed

5 files changed

+3509
-1822
lines changed

fixtures/concurrent/time-slicing/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ There are also known bugs and inefficiencies in master so **don't use this fixtu
1414

1515
## How do I run this fixture?
1616

17+
### From npm version
18+
19+
```
20+
# 1: Install fixture dependencies
21+
cd fixtures/unstable-async/time-slicing/
22+
yarn
23+
24+
# 2: Run the app
25+
yarn start
26+
```
27+
28+
### From React source code
1729
```shell
1830
# 1: Build react from source
1931
cd /path/to/react
@@ -24,6 +36,9 @@ yarn build react-dom/index,react/index,react-cache,scheduler --type=NODE
2436
cd fixtures/unstable-async/time-slicing/
2537
yarn
2638

39+
# 3: Copy React source code over
40+
yarn copy-source
41+
2742
# 3: Run the app
2843
yarn start
2944
```

fixtures/concurrent/time-slicing/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"private": true,
55
"dependencies": {
66
"glamor": "^2.20.40",
7+
"react": "0.0.0-experimental-269dd6ec5",
8+
"react-dom": "0.0.0-experimental-269dd6ec5",
79
"react-markdown": "^3.2.0",
810
"react-scripts": "^1.1.4",
911
"victory": "^0.25.6"
1012
},
1113
"scripts": {
12-
"prestart": "cp -r ../../../build/node_modules/* ./node_modules/",
13-
"prebuild": "cp -r ../../../build/node_modules/* ./node_modules/",
14+
"copy-source": "cp -r ../../../build/node_modules/* ./node_modules/",
1415
"start": "react-scripts start",
1516
"build": "react-scripts build",
1617
"test": "react-scripts test --env=jsdom",

fixtures/concurrent/time-slicing/src/index.css

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@ body {
3232
margin-top: 20px;
3333
margin-bottom: 20px;
3434
zoom: 1.8;
35+
text-align: center;
36+
display: flex;
37+
justify-content: space-between;
3538
}
3639

3740
label {
3841
zoom: 1;
39-
margin-right: 50px;
4042
font-size: 30px;
4143
}
4244

4345
label.selected {
44-
font-weight: bold;
46+
font-weight: bold;
4547
}
4648

4749
label:nth-child(1).selected {
@@ -56,6 +58,10 @@ label:nth-child(3).selected {
5658
color: #61dafb;
5759
}
5860

61+
input[type="radio" i]:nth-child(1) {
62+
margin-left: 0;
63+
}
64+
5965
.chart {
6066
width: 100%;
6167
height: 100%;

fixtures/concurrent/time-slicing/src/index.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React, {PureComponent} from 'react';
2-
import {flushSync, createRoot} from 'react-dom';
3-
import Scheduler from 'scheduler';
1+
import React, {PureComponent, unstable_startTransition} from 'react';
2+
import {unstable_createRoot} from 'react-dom';
43
import _ from 'lodash';
54
import Charts from './Charts';
65
import Clock from './Clock';
@@ -55,19 +54,17 @@ class App extends PureComponent {
5554
return;
5655
}
5756
if (this.state.strategy !== 'async') {
58-
flushSync(() => {
59-
this.setState(state => ({
60-
showDemo: !state.showDemo,
61-
}));
62-
});
57+
this.setState(state => ({
58+
showDemo: !state.showDemo,
59+
}));
6360
return;
6461
}
6562
if (this._ignoreClick) {
6663
return;
6764
}
6865
this._ignoreClick = true;
6966

70-
Scheduler.unstable_next(() => {
67+
unstable_startTransition(() => {
7168
this.setState({showDemo: true}, () => {
7269
this._ignoreClick = false;
7370
});
@@ -76,9 +73,7 @@ class App extends PureComponent {
7673

7774
debouncedHandleChange = _.debounce(value => {
7875
if (this.state.strategy === 'debounced') {
79-
flushSync(() => {
80-
this.setState({value: value});
81-
});
76+
this.setState({value: value});
8277
}
8378
}, 1000);
8479

@@ -108,9 +103,9 @@ class App extends PureComponent {
108103
break;
109104
case 'async':
110105
// TODO: useTransition hook instead.
111-
setTimeout(() => {
106+
unstable_startTransition(() => {
112107
this.setState({value});
113-
}, 0);
108+
});
114109
break;
115110
default:
116111
break;
@@ -147,5 +142,5 @@ class App extends PureComponent {
147142
}
148143

149144
const container = document.getElementById('root');
150-
const root = createRoot(container);
145+
const root = unstable_createRoot(container);
151146
root.render(<App />);

0 commit comments

Comments
 (0)