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

@uppy/react: deprecate useUppy #4223

Merged
merged 7 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions packages/@uppy/react/src/useUppy.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type Uppy from '@uppy/core'

/**
* @deprecated Initialize Uppy outside of the component or with `useState` and `useEffect` (see docs)
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
*/
declare function useUppy(factory: () => Uppy): Uppy

export default useUppy
3 changes: 3 additions & 0 deletions packages/@uppy/react/src/useUppy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useEffect, useRef } from 'react'
import { Uppy as UppyCore } from '@uppy/core'

/**
* @deprecated Initialize Uppy outside of the component or with `useState` and `useEffect` (see docs)
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
*/
export default function useUppy (factory) {
if (typeof factory !== 'function') {
throw new TypeError('useUppy: expected a function that returns a new Uppy instance')
Expand Down
63 changes: 0 additions & 63 deletions website/src/docs/react-initializing.md

This file was deleted.

87 changes: 45 additions & 42 deletions website/src/docs/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ Install from NPM:
npm install @uppy/react
```

## CSS

Make sure to also include the necessary CSS files for each Uppy React component you are using. Follow links for individual components docs below for details on that.

## Usage

The components can be used with either [React][] or API-compatible alternatives such as [Preact][].
Expand All @@ -31,48 +27,55 @@ Instead of adding a UI plugin to an Uppy instance with `.use()`, the Uppy instan
All other props are passed as options to the plugin.

```js
import React from 'react'
import React, { useEffect } from 'react'
import Uppy from '@uppy/core'
import Tus from '@uppy/tus'
import { DragDrop } from '@uppy/react'

const uppy = new Uppy({
meta: { type: 'avatar' },
restrictions: { maxNumberOfFiles: 1 },
autoProceed: true,
})

uppy.use(Tus, { endpoint: '/upload' })

uppy.on('complete', (result) => {
const url = result.successful[0].uploadURL
store.dispatch({
type: 'SET_USER_AVATAR_URL',
payload: { url },
})
})

const AvatarPicker = ({ currentAvatar }) => {
return (
<div>
<img src={currentAvatar} alt="Current Avatar" />
<DragDrop
uppy={uppy}
locale={{
strings: {
// Text to show on the droppable area.
// `%{browse}` is replaced with a link that opens the system file selection dialog.
dropHereOr: 'Drop here or %{browse}',
// Used as the label for the link that opens the system file selection dialog.
browse: 'browse',
},
}}
/>
</div>
)
import { Dashboard } from '@uppy/react'

const uppy = new Uppy()

function Component () {
useEffect(() => {
return () => {
uppy.close({ reason: 'unmount' })
}
}, [])

return <Dashboard uppy={uppy} />
}
```

### Dynamic options

```js
import React, { useState, useEffect } from 'react'
import Uppy from '@uppy/core'
import { Dashboard } from '@uppy/react'
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to highlight import Dashboard from '@uppy/react/lib/Dashboard.js'? We probably want to introduce an exports map to sugar code it, but importing the individual modules makes the most sense otherwise you have to fight with peer dependencies.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was hoping to do a separate PR for the exports map next to this so we don't have to document that workaround


const uppy = new Uppy()

function Component ({ restrictions }) {
useEffect(() => {
// Change @uppy/core options
uppy.setOptions({ restrictions })

// Or change some plugin dynamically
uppy.getPlugin('SomePlugin').setOptions({ /* options */ })

return () => {
uppy.close({ reason: 'unmount' })
}
}, [restrictions])

return <Dashboard uppy={uppy} />
}
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
```

## CSS

Make sure to also include the necessary CSS files for each Uppy React component you are using. Follow links for individual components docs below for details on that.

## Components

The following plugins are available as React component wrappers (you need to
install each package separately):

Expand Down