Skip to content

Commit 5692649

Browse files
committed
feat: example code
1 parent a3d475d commit 5692649

7 files changed

Lines changed: 113 additions & 0 deletions

File tree

examples/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Example
2+
3+
## How to Use
4+
5+
Install it and run:
6+
7+
```bash
8+
pnpm install
9+
pnpm dev
10+
```

examples/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<title>usePolling Example</title>
7+
</head>
8+
<body>
9+
<noscript>You need to enable JavaScript to run this app.</noscript>
10+
<div id="root"></div>
11+
12+
<script src="/src/index.tsx" type="module"></script>
13+
</body>
14+
</html>

examples/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "exmaple",
3+
"private": true,
4+
"license": "MIT",
5+
"dependencies": {
6+
"react": "18.2.0",
7+
"react-dom": "18.2.0"
8+
},
9+
"devDependencies": {
10+
"@types/react": "18.0.26",
11+
"@types/react-dom": "18.0.10",
12+
"@vitejs/plugin-react-swc": "3.0.1",
13+
"typescript": "4.9.4",
14+
"vite": "4.0.3"
15+
},
16+
"scripts": {
17+
"dev": "vite"
18+
}
19+
}

examples/src/app.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useState, type FC } from 'react'
2+
import { usePolling } from '../../src'
3+
4+
export const App: FC = () => {
5+
const [count, setCount] = useState(0)
6+
7+
const updateCount = async (): Promise<void> => {
8+
await sleep(100)
9+
10+
setCount(c => c + 1)
11+
}
12+
13+
const { isPaused, pause, resume } = usePolling(updateCount, 1000)
14+
15+
return (
16+
<>
17+
<div>Polling: {isPaused ? 'OFF' : 'ON'}</div>
18+
<div>Count: {count}</div>
19+
<button onClick={() => pause()} disabled={isPaused}>
20+
Pause
21+
</button>
22+
<button onClick={() => resume()} disabled={!isPaused}>
23+
Resume
24+
</button>
25+
</>
26+
)
27+
}
28+
29+
const sleep = (ms: number): Promise<unknown> => new Promise(res => setTimeout(res, ms))

examples/src/index.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { StrictMode } from 'react'
2+
import { createRoot } from 'react-dom/client'
3+
import { App } from './app'
4+
5+
const rootElement = window.document.getElementById('root')
6+
if (!rootElement) throw new Error('Failed to find the root element')
7+
const root = createRoot(rootElement)
8+
9+
root.render(
10+
<StrictMode>
11+
<App />
12+
</StrictMode>
13+
)

examples/tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"target": "ESNext",
5+
"module": "ESNext",
6+
"moduleResolution": "node",
7+
"jsx": "react-jsx",
8+
"types": ["react"],
9+
"typeRoots": ["node_modules/@types", "src/types"],
10+
"strict": true,
11+
"exactOptionalPropertyTypes": true,
12+
"esModuleInterop": true,
13+
"forceConsistentCasingInFileNames": true,
14+
"skipLibCheck": true,
15+
"isolatedModules": true,
16+
"noEmit": true,
17+
"noFallthroughCasesInSwitch": true,
18+
"noImplicitReturns": true,
19+
"noUnusedLocals": true,
20+
"noUnusedParameters": true
21+
}
22+
}

examples/vite.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import reactPlugin from '@vitejs/plugin-react-swc'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
plugins: [reactPlugin()],
6+
})

0 commit comments

Comments
 (0)