Giter VIP home page Giter VIP logo

react-native-activestorage's Introduction

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 rn-fetch-blob

Usage

Add Active Storage provider

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

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

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

Use with React Hooks

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 (
    <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'

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'

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.

react-native-activestorage's People

Contributors

jpalumickas avatar masterlambaster avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.