Skip to content

Commit 2d2a669

Browse files
committed
Move typing hacks to experimental file
1 parent 86fc7e8 commit 2d2a669

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/App.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import randomWords from "random-words";
22
import { v4 as uuid } from "uuid";
3-
import * as React from "react";
3+
import React, { useMemo, useState, useEffect, useCallback } from "react";
44
import { connectToDB, getConnection } from "./sharedb";
5+
import { useTransition } from "./react-experimental";
56
import {
67
Link,
78
BrowserRouter as Router,
89
useHistory,
910
useLocation,
1011
} from "react-router-dom";
1112

12-
const useTransition: () => [(fn: Function) => void, boolean] = (React as any)
13-
.unstable_useTransition;
14-
1513
interface Node {
1614
text: string;
1715
}
@@ -35,11 +33,11 @@ function useFlow(config: {
3533

3634
const [startTransition, isPending] = useTransition();
3735

38-
const [state, setState] = React.useState<Flow | null>(null);
36+
const [state, setState] = useState<Flow | null>(null);
3937

40-
const doc = React.useMemo(() => getConnection(config.id), [config.id]);
38+
const doc = useMemo(() => getConnection(config.id), [config.id]);
4139

42-
React.useEffect(() => {
40+
useEffect(() => {
4341
const cloneStateFromShareDB = () =>
4442
startTransition(() => {
4543
setState(JSON.parse(JSON.stringify(doc.data)));
@@ -58,18 +56,18 @@ function useFlow(config: {
5856

5957
// Methods
6058

61-
const addNode = React.useCallback(() => {
59+
const addNode = useCallback(() => {
6260
doc.submitOp([{ p: ["nodes", uuid()], oi: { text: randomWords() } }]);
6361
}, [doc]);
6462

65-
const removeNode = React.useCallback(
63+
const removeNode = useCallback(
6664
(id) => {
6765
doc.submitOp([{ p: ["nodes", id], od: {} }]);
6866
},
6967
[doc]
7068
);
7169

72-
const reset = React.useCallback(
70+
const reset = useCallback(
7371
(flow) => {
7472
doc.submitOp([{ p: [], od: doc.data, oi: flow }]);
7573
},
@@ -180,15 +178,15 @@ const App = () => {
180178
const history = useHistory();
181179
const location = useLocation();
182180

183-
const id = React.useMemo(() => {
181+
const id = useMemo(() => {
184182
if (location.hash.length < 2) {
185183
return null;
186184
}
187185
return location.hash.slice(1);
188186
}, [location]);
189187

190188
// If there is no ID readable from the hash, redirect to a freshly created one
191-
React.useEffect(() => {
189+
useEffect(() => {
192190
if (id === null) {
193191
history.push(`#${randomWords()}`);
194192
}

src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from "react";
2-
import * as ReactDOM from "react-dom";
2+
import { createRoot } from "./react-experimental";
33
import App from "./App";
44
import * as serviceWorker from "./serviceWorker";
55
import "./style.css";
66

7-
(ReactDOM as any).unstable_createRoot(document.getElementById("root")).render(
7+
createRoot(document.getElementById("root")).render(
88
<React.StrictMode>
99
<App />
1010
</React.StrictMode>

src/react-experimental.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as React from "react";
2+
import * as ReactDOM from "react-dom";
3+
4+
/**
5+
* Typing hacks for experimental concurrent mode features in React
6+
*/
7+
8+
export const createRoot = (ReactDOM as any).unstable_createRoot;
9+
10+
export const useTransition: () => [
11+
(fn: Function) => void,
12+
boolean
13+
] = (React as any).unstable_useTransition;

0 commit comments

Comments
 (0)