Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: make React example up-to-date #5205

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 0 additions & 114 deletions examples/react-example/App.jsx

This file was deleted.

49 changes: 49 additions & 0 deletions examples/react-example/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable */
import React from 'react'
import Uppy from '@uppy/core'
import Tus from '@uppy/tus'
import Webcam from '@uppy/webcam'
import RemoteSources from '@uppy/remote-sources'
import { Dashboard, useUppyState } from '@uppy/react'

import '@uppy/core/dist/style.css'
import '@uppy/dashboard/dist/style.css'
import '@uppy/drag-drop/dist/style.css'
import '@uppy/file-input/dist/style.css'
import '@uppy/progress-bar/dist/style.css'

const metaFields = [
{ id: 'license', name: 'License', placeholder: 'specify license' },
]

function createUppy() {
return new Uppy({ restrictions: { requiredMetaFields: ['license'] } })
.use(Tus, { endpoint: 'https://tusd.tusdemo.net/files/' })
.use(Webcam)
.use(RemoteSources, {
companionUrl: 'https://companion.uppy.io',
})
}

export default function App() {
// IMPORTANT: passing an initaliser function to useState
// to prevent creating a new Uppy instance on every render.
// useMemo is a performance hint, not a guarantee.
const [uppy] = React.useState(createUppy)
// You can access state reactively with useUppyState
const fileCount = useUppyState(
uppy,
(state) => Object.keys(state.files).length,
)
const totalProgress = useUppyState(uppy, (state) => state.totalProgress)
// Also possible to get the state of all plugins.
const plugins = useUppyState(uppy, (state) => state.plugins)
Comment on lines +34 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this could be simplified as

Suggested change
const fileCount = useUppyState(
uppy,
(state) => Object.keys(state.files).length,
)
const totalProgress = useUppyState(uppy, (state) => state.totalProgress)
// Also possible to get the state of all plugins.
const plugins = useUppyState(uppy, (state) => state.plugins)
const { fileCount, totalProgress, plugins } = useUppyState(
uppy,
(state) => ({
fileCount: Object.keys(state.files).length,
totalProgress: state.totalProgress,
plugins: state.plugins,
// ... anything else you might need
}),
)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of an anti-pattern in the selector/lenses pattern. Selector should have a single purpose and the selector determines how often the hook needs to re-render based on the subset of state it returns, which is very big and deep in your case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should add a comment about that


return (
<>
<p>File count: {fileCount}</p>
<p>Total progress: {totalProgress}</p>
<Dashboard uppy={uppy} metaFields={metaFields} />
</>
)
}
2 changes: 1 addition & 1 deletion examples/react-example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
</head>
<body>
<div id="app"></div>
<script src="./main.jsx" type="module"></script>
<script src="./main.tsx" type="module"></script>
</body>
</html>
8 changes: 0 additions & 8 deletions examples/react-example/main.jsx

This file was deleted.

6 changes: 6 additions & 0 deletions examples/react-example/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable */
import React from 'react'
import { createRoot } from 'react-dom/client'
import App from './App.tsx'

createRoot(document.querySelector('#app')).render(<App />)
5 changes: 1 addition & 4 deletions examples/react-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
"dependencies": {
"@uppy/core": "workspace:*",
"@uppy/dashboard": "workspace:*",
"@uppy/drag-drop": "workspace:*",
"@uppy/file-input": "workspace:*",
"@uppy/google-drive": "workspace:*",
"@uppy/progress-bar": "workspace:*",
"@uppy/react": "workspace:*",
"@uppy/remote-sources": "workspace:*",
"@uppy/tus": "workspace:*",
"react": "^18.0.0",
"react-dom": "^18.0.0"
Expand Down
21 changes: 21 additions & 0 deletions examples/react-example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"jsxImportSource": "react",
"jsx": "react-jsx",
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2022",
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
"isolatedModules": true,
"verbatimModuleSyntax": true,
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"module": "NodeNext",
"outDir": "dist",
"sourceMap": true,
"lib": ["es2022", "dom", "dom.iterable"],
},
}
7 changes: 2 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8659,11 +8659,8 @@ __metadata:
dependencies:
"@uppy/core": "workspace:*"
"@uppy/dashboard": "workspace:*"
"@uppy/drag-drop": "workspace:*"
"@uppy/file-input": "workspace:*"
"@uppy/google-drive": "workspace:*"
"@uppy/progress-bar": "workspace:*"
"@uppy/react": "workspace:*"
"@uppy/remote-sources": "workspace:*"
"@uppy/tus": "workspace:*"
"@vitejs/plugin-react": "npm:^4.0.0"
react: "npm:^18.0.0"
Expand Down Expand Up @@ -9040,7 +9037,7 @@ __metadata:
languageName: unknown
linkType: soft

"@uppy/file-input@workspace:*, @uppy/file-input@workspace:^, @uppy/file-input@workspace:packages/@uppy/file-input":
"@uppy/file-input@workspace:^, @uppy/file-input@workspace:packages/@uppy/file-input":
version: 0.0.0-use.local
resolution: "@uppy/file-input@workspace:packages/@uppy/file-input"
dependencies:
Expand Down