Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jpalumickas/react-native-activestorage

React Native support for Rails ActiveStorage
https://github.com/jpalumickas/react-native-activestorage

activestorage rails react react-native

Last synced: 2 months ago
JSON representation

React Native support for Rails ActiveStorage

Awesome Lists containing this project

README

        

# React Native ActiveStorage

Use direct upload for uploading files to Rails ActiveStorage.

## Installation

Install this package and [react-native-blob-util](react-native-blob-util)

```sh
yarn add react-native-activestorage react-native-blob-util
```

## Usage

### Add Active Storage provider

```jsx
import { ActiveStorageProvider } from 'react-native-activestorage'

const App = () => (



)
```

You can provide `mountPath` to provider if you have different than `/rails/active_storage`

### Use with React Hooks

```jsx
import { useDirectUpload } from 'react-native-activestorage'

const Upload = () => {
const onSuccess = ({ signedIds }) => {
// Do something;
}

const { upload, uploading, uploads } = useDirectUpload({ onSuccess });

const onUploadButtonClick = async () => {
const files = await showPicker();
const { signedIds } = await upload(files);
// Assign signed IDs
}

return (


Upload

{uploads.map(upload => (

))}

)
}
```

### Use with React Component

```jsx
import { DirectUpload } from 'react-native-activestorage'

const Upload = () => (
console.warn({ signedIds })}>
{({ upload, uploading, uploads }) => (

showPicker((files) => upload(files))}>
Upload

{uploads.map(upload => )}

)}

)
```

### Use `directUpload` without React

```js
import { directUpload } from 'react-native-activestorage'

const file = {
name: 'image.jpg',
size: 123456,
type: 'image/jpeg',
path: '/var/lib/...'
}

const directUploadsUrl = 'https://localhost:3000/rails/active_storage/direct_uploads';

const onStatusChange = ({ status, progress, cancel }) => {
// status - waiting/uploading/success/error/canceled
// progress - 0-100% (for uploading status)
// cancel - function to stop uploading a file
}

directUpload({ file, directUploadsUrl, onStatusChange });
```

## License

The package is available as open source under the terms of the [MIT License][license].

[license]: https://raw.githubusercontent.com/jpalumickas/react-native-activestorage/master/LICENSE
[react-native-blob-util]: https://www.npmjs.com/package/react-native-blob-util