Gathering detailed insights and metrics for @rbngzlv/react-activestorage-provider
Gathering detailed insights and metrics for @rbngzlv/react-activestorage-provider
Gathering detailed insights and metrics for @rbngzlv/react-activestorage-provider
Gathering detailed insights and metrics for @rbngzlv/react-activestorage-provider
A React component that allows easy file upload using ActiveStorage
npm install @rbngzlv/react-activestorage-provider
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
68 Commits
10 Branches
1 Contributors
Updated on Feb 02, 2020
Latest Version
6.0.0
Package Id
@rbngzlv/react-activestorage-provider@6.0.0
Unpacked Size
202.68 kB
Size
47.12 kB
File Count
17
NPM Version
6.13.4
Node Version
12.14.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
2
34
ActiveStorage is an amazing addition to Rails 5.2, and as usual the team have made it fantastically simple to use... if you’re generating HTML server-side, that is. This component aims to make it just as easy to use from React.
ActiveStorageProvider handles the direct upload of a file to an ActiveStorage service and the attachment of that file to your model. It uses the render props pattern so you can build your own upload widget.
1npm install --save react-activestorage-provider
ActiveStorageProvider makes it easy to add a simple upload button. When you call handleUpload
with a FileList
or an array of File
s, this component creates a Blob
record, uploads the file directly to your storage service, and then hits your Rails controller to attach the blob to your model. (If you want to handle the attachment yourself in order to, for example, provide other attributes, see the lower level DirectUploadProvider
.)
1import ActiveStorageProvider from 'react-activestorage-provider' 2 3// ... 4 5return ( 6 <ActiveStorageProvider 7 endpoint={{ 8 path: '/profile', 9 model: 'User', 10 attribute: 'avatar', 11 method: 'PUT', 12 }} 13 onSubmit={user => this.setState({ avatar: user.avatar })} 14 render={({ handleUpload, uploads, ready }) => ( 15 <div> 16 <input 17 type="file" 18 disabled={!ready} 19 onChange={e => handleUpload(e.currentTarget.files)} 20 /> 21 22 {uploads.map(upload => { 23 switch (upload.state) { 24 case 'waiting': 25 return <p key={upload.id}>Waiting to upload {upload.file.name}</p> 26 case 'uploading': 27 return ( 28 <p key={upload.id}> 29 Uploading {upload.file.name}: {upload.progress}% 30 </p> 31 ) 32 case 'error': 33 return ( 34 <p key={upload.id}> 35 Error uploading {upload.file.name}: {upload.error} 36 </p> 37 ) 38 case 'finished': 39 return ( 40 <p key={upload.id}>Finished uploading {upload.file.name}</p> 41 ) 42 } 43 })} 44 </div> 45 )} 46 /> 47)
ActiveStorageProvider
PropsThese are your options for configuring ActiveStorageProvider.
Prop (*required) | Description |
---|---|
directUploadsPath | string The direct uploads path on your Rails app, if you’ve overridden ActiveStorage::DirectUploadsController |
endpoint * | { path: string, model: string, attribute: string, method: string, host?: string, port?: string, protocol?: string } The details for the request to attach the file |
headers | {[key: string]: string} Optional headers to add to request, can also be used to override default headers |
multiple | boolean (false)Whether the component should accept multiple files. If true, the model should use has_many_attached |
onBeforeBlobRequest | ({ id: string, file: File, xhr: XMLHttpRequest }) => mixed A callback that allows you to modify the blob request |
onBeforeStorageRequest | ({ id: string, file: File, xhr: XMLHttpRequest }) => mixed A callback that allows you to modify the storage request |
onError | Response => mixed A callback to handle an error (>= 400) response by the server in saving your model |
onSubmit * | Object => mixed A callback for the server response to successfully saving your model |
render * | RenderProps => React.Node Render props |
RenderProps
This is the type of the argument with which your render function will be called.
1export type RenderProps = { 2 ready: boolean /* false while any file is uploading */, 3 uploads: ActiveStorageFileUpload[] /* uploads in progress */, 4 5 handleUpload: (FileList | File[]) => mixed /* call to initiate an upload */, 6 7 /* or, if you want more granular control... */ 8 9 /* call to set list of files to be uploaded */ 10 handleChooseFiles: (FileList | File[]) => mixed, 11 /* then call to begin the upload of the files in the list */ 12 handleBeginUpload: () => mixed, 13} 14 15type ActiveStorageFileUpload = 16 | { state: 'waiting', id: string, file: File } 17 | { state: 'uploading', id: string, file: File, progress: number } 18 | { state: 'error', id: string, file: File, error: string } 19 | { state: 'finished', id: string, file: File }
DirectUploadProvider
ActiveStorageProvider makes it simple to add a quick “upload” button by taking care of both uploading and attaching your file, but it shouldn’t stand in your way if you’re doing something more interesting. If you want to handle the second step, attaching your Blob
record to your model, yourself, you can use the lower level DirectUploadProvider
. It creates the blob records and uploads the user’s files directly to your storage service, then calls you back with the signed ids of those blobs. Then, you can create or update your model as you need.
1function PostForm() { 2 function handleAttachment(signedIds) { 3 const body = JSON.stringify({ post: { title: ..., images: signedIds }}) 4 fetch('/posts.json', { method: 'POST', body }) 5 } 6 7 return ( 8 <DirectUploadProvider multiple onSuccess={handleAttachment} render={...} /> 9 ) 10}
DirectUploadProvider
is a named export, so
1import { DirectUploadProvider } from 'react-activestorage-provider'
and use it with the following props:
Prop (*required) | Description |
---|---|
directUploadsPath | string The direct uploads path on your Rails app, if you’ve overridden ActiveStorage::DirectUploadsController |
headers | {[key: string]: string} Optional headers to add to request |
multiple | boolean Whether the component should accept multiple files. If true, the model should use has_many_attached |
onBeforeBlobRequest | ({ id: string, file: File, xhr: XMLHttpRequest }) => mixed A callback that allows you to modify the blob request |
onBeforeStorageRequest | ({ id: string, file: File, xhr: XMLHttpRequest }) => mixed A callback that allows you to modify the storage request |
onSuccess * | (string[]) => mixed The callback that will be called with the signed ids of the files after the upload is complete |
origin | { host?: string, port?: string, protocol?: string } The origin of your rails server. Defaults to where your React app is running |
render * | RenderProps => React.Node Render props |
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
no SAST tool detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
67 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More