Skip to content

Commit

Permalink
use react lazy & Suspense
Browse files Browse the repository at this point in the history
  • Loading branch information
qsliu2017 committed Jan 14, 2024
1 parent e6fe343 commit 312e5c0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
Binary file modified bun.lockb
Binary file not shown.
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
"format": "prettier --write ."
},
"dependencies": {
"csv-parse": "^5.5.3",
"preact": "^10.19.3"
"react": "^18.2.0",
"react-dom": "^18.2.0",
"csv-parse": "^5.5.3"
},
"devDependencies": {
"@preact/preset-vite": "^2.7.0",
"prettier": "^3.1.1",
"@types/react": "^18.2.47",
"@types/react-dom": "^18.2.18",
"@vitejs/plugin-react": "^4.2.1",
"prettier": "^3.2.2",
"vite": "^5.0.8"
}
}
2 changes: 1 addition & 1 deletion src/csv.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CsvError, parse } from "csv-parse/browser/esm";
import { useCallback, useEffect, useState } from "preact/hooks";
import { useCallback, useEffect, useState } from "react";

export default function CsvSplit() {
const [input, setInput] = useState('"a","b","c"\n"1\n10","2","3"');
Expand Down
27 changes: 16 additions & 11 deletions src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import { render } from "preact";
import { useState } from "preact/hooks";
import CsvSplit from "./csv";
import StringToUtf8CodePoint from "./utf";
import { Suspense, lazy, render, useState } from "react";

const layouts = new Map([
["utf", (key) => <StringToUtf8CodePoint key={key} />],
["csv", (key) => <CsvSplit key={key} />],
]);
const CsvSplit = lazy(() => import("./csv"));
const StringToUtf8CodePoint = lazy(() => import("./utf"));

const layouts = ["utf", "csv"];

render(<App />, document.getElementById("app"));

function App() {
const [layout, setLayout] = useState(layouts.keys().next()?.value);
const [layout, setLayout] = useState(layouts[0]);
return (
<>
<select value={layout} onChange={(e) => setLayout(e.target.value)}>
{Array.from(layouts.keys()).map((key) => (
{layouts.map((key) => (
<option value={key}>{key}</option>
))}
</select>
{layouts.get(layout)(layout)}
<Suspense fallback={<div>Loading...</div>}>
{layout === "utf" ? (
<StringToUtf8CodePoint key="utf" />
) : layout === "csv" ? (
<CsvSplit key="csv" />
) : (
<div>Unknown</div>
)}
</Suspense>
</>
);
}
2 changes: 1 addition & 1 deletion src/utf.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "preact/hooks";
import { useState } from "react";
export default function StringToUtf8CodePoint() {
const [input, setInput] = useState("English(us)\n中文(简体) \n🤣");
return (
Expand Down

0 comments on commit 312e5c0

Please sign in to comment.