react-native-activestorage-floranow
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

React Native ActiveStorage

Use direct upload for uploading files to Rails ActiveStorage.

Installation

Install this package and rn-fetch-blob

yarn add react-native-activestorage-floranow rn-fetch-blob

Usage

Add Active Storage provider

import { ActiveStorageProvider } from 'react-native-activestorage-floranow'

const App = () => (
  <ActiveStorageProvider host="https://localhost:4000">
    <Navigation />
  </ActiveStorageProvider>
)

You can provide mountPath to provider if you have different than /rails/active_storage and you can provide interval to provider if you want to change streaming interval rather than 2000 ms (millisecond)

Use with React Hooks

import { useDirectUpload } from 'react-native-activestorage-floranow'

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 (
    <View>
      <View onClick={onUploadButtonClick}>
        <Text>Upload</Text>
      </View>

      {uploads.map(upload => (
        <UploadItem upload={upload} key={upload.id} />
      ))}
    </View>
  )
}

Use with React Component

import { DirectUpload } from 'react-native-activestorage-floranow'

const Upload = () => (
  <DirectUpload onSuccess={({ signedIds }) => console.warn({ signedIds })}>
    {({ upload, uploading, uploads }) => (
      <View>
        <View onClick={() => showPicker((files) => upload(files))}>
          <Text>Upload</Text>
        </View>
        {uploads.map(upload => <UploadItem upload={upload} key={upload.id} />)}
      </View>
    )}
  </DirectUpload>
)

Use directUpload without React

import { directUpload } from 'react-native-activestorage-floranow'

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 });

Fixes

Fix stream closed error occurred in Android.

License

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

Dependencies (1)

Dev Dependencies (6)

Package Sidebar

Install

npm i react-native-activestorage-floranow

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

63.2 kB

Total Files

31

Last publish

Collaborators

  • moayad_alomari