Skip to content

Commit

Permalink
chore: Improve uploadData snippets for React
Browse files Browse the repository at this point in the history
  • Loading branch information
cshfang committed Jan 30, 2025
1 parent a4b1198 commit 61384ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -790,30 +790,31 @@ class MyApp extends StatefulWidget {
Next, let's a photo to the `picture-submissions/` path.

<InlineFilter filters={["react", "react-native"]}>
```javascript
```jsx
import React from 'react';
import { uploadData } from 'aws-amplify/storage';
function App() {
const [file, setFile] = React.useState();
const handleChange = (event: any) => {
setFile(event.target.files[0]);
const handleChange = (event) => {
setFile(event.target.files?.[0]);
};
const handleClick = () => {
if (!file) {
return;
}
uploadData({
path: `picture-submissions/${file.name}`,
data: file,
});
};

return (
<div>
<input type="file" onChange={handleChange} />
<button
onClick={() =>
uploadData({
path: `picture-submissions/${file.name}`,
data: file,
})
}
>
Upload
</button>
<button onClick={handleClick}>Upload</button>
</div>
);
}
Expand Down
45 changes: 23 additions & 22 deletions src/pages/[platform]/build-a-backend/storage/upload-files/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,33 @@ Learn more about how you can further customize the UI component by referring to
The following is an example of how you would upload a file from a file object, this could be retrieved from the local machine or a different source.

<InlineFilter filters={["react", "react-native"]}>
```javascript
```jsx
import React from 'react';
import { uploadData } from 'aws-amplify/storage';

function App() {
const [file, setFile] = React.useState();

const handleChange = (event: any) => {
setFile(event.target.files[0]);
};

return (
<div>
<input type="file" onChange={handleChange} />
<button
onClick={() =>
uploadData({
path: `photos/${file.name}`,
data: file,
})
}
>
Upload
</button>
</div>
);
const [file, setFile] = React.useState();

const handleChange = (event) => {
setFile(event.target.files?.[0]);
};

const handleClick = () => {
if (!file) {
return;
}
uploadData({
path: `photos/${file.name}`,
data: file,
});
};

return (
<div>
<input type="file" onChange={handleChange} />
<button onClick={handleClick}>Upload</button>
</div>
);
}
```
</InlineFilter>
Expand Down

0 comments on commit 61384ae

Please sign in to comment.