-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
79 lines (64 loc) · 1.95 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import * as React from "react";
import * as ReactDOM from "react-dom";
import { unstable_createResource as createResource } from "react-cache";
import { unstable_scheduleCallback } from "scheduler";
import { getText } from "./getText";
const readText = createResource(getText);
function Text({ value }) {
return <span>{value}</span>;
}
function AsyncText({ value }) {
value = readText.read(value);
return <Text value={value} />;
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
value: 0,
valueAsync: 0
};
readText.preload(0);
}
addOne = () => {
// High-priority update to `state.value`
this.setState({ value: this.state.value + 1 });
// Low priority update to `state.valueAsync`. Could be suspended.
unstable_scheduleCallback(() =>
this.setState({ valueAsync: this.state.valueAsync + 1 })
);
};
substractOne = () => {
// High-priority update to `state.value`
this.setState({ value: this.state.value - 1 });
// Low priority update to `state.valueAsync`. Could be suspended.
unstable_scheduleCallback(() =>
this.setState({ valueAsync: this.state.valueAsync - 1 })
);
};
render() {
const { value, valueAsync } = this.state;
return (
<React.Fragment>
<h1>Async Text Suspense Demo</h1>
<div className="button-bar">
<button onClick={this.addOne}>+1</button>
<button onClick={this.substractOne}>-1</button>
</div>
<div>
<span>Expected: </span>
<Text value={value} />
</div>
<div>
<span>AsyncText: </span>
<React.Suspense maxDuration={2500} fallback={<span>Loading...</span>}>
<AsyncText value={valueAsync} />
</React.Suspense>
</div>
</React.Fragment>
);
}
}
const container = document.getElementById("root");
const root = ReactDOM.unstable_createRoot(container);
root.render(<App />);