File tree Expand file tree Collapse file tree 2 files changed +37
-35
lines changed
src/pages/[platform]/build-a-backend/storage Expand file tree Collapse file tree 2 files changed +37
-35
lines changed Original file line number Diff line number Diff line change @@ -790,30 +790,31 @@ class MyApp extends StatefulWidget {
790790Next, let's a photo to the `picture-submissions/` path.
791791
792792<InlineFilter filters={["react", "react-native"]}>
793- ` ` ` javascript
793+ ` ` ` jsx
794794import React from 'react';
795795import { uploadData } from 'aws-amplify/storage';
796796
797797function App() {
798798 const [file, setFile] = React.useState();
799799
800- const handleChange = (event: any) => {
801- setFile(event.target.files[0]);
800+ const handleChange = (event) => {
801+ setFile(event.target.files?.[0]);
802+ };
803+
804+ const handleClick = () => {
805+ if (!file) {
806+ return;
807+ }
808+ uploadData({
809+ path: ` picture-submissions/${file.name}`,
810+ data : file,
811+ });
802812 };
803813
804814 return (
805815 <div>
806816 <input type="file" onChange={handleChange} />
807- <button
808- onClick={() =>
809- uploadData({
810- path: ` picture-submissions/${file.name}`,
811- data : file,
812- })
813- }
814- >
815- Upload
816- </button>
817+ <button onClick={handleClick}>Upload</button>
817818 </div>
818819 );
819820}
Original file line number Diff line number Diff line change @@ -82,32 +82,33 @@ Learn more about how you can further customize the UI component by referring to
8282The 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.
8383
8484<InlineFilter filters = { [" react" , " react-native" ]} >
85- ``` javascript
85+ ``` jsx
8686import React from ' react' ;
8787import { uploadData } from ' aws-amplify/storage' ;
8888
8989function App () {
90- const [file , setFile ] = React .useState ();
91-
92- const handleChange = (event : any ) => {
93- setFile (event .target .files [0 ]);
94- };
95-
96- return (
97- < div>
98- < input type= " file" onChange= {handleChange} / >
99- < button
100- onClick= {() =>
101- uploadData ({
102- path: ` photos/${ file .name } ` ,
103- data: file,
104- })
105- }
106- >
107- Upload
108- < / button>
109- < / div>
110- );
90+ const [file , setFile ] = React .useState ();
91+
92+ const handleChange = (event ) => {
93+ setFile (event .target .files ? .[0 ]);
94+ };
95+
96+ const handleClick = () => {
97+ if (! file) {
98+ return ;
99+ }
100+ uploadData ({
101+ path: ` photos/${ file .name } ` ,
102+ data: file,
103+ });
104+ };
105+
106+ return (
107+ < div>
108+ < input type= " file" onChange= {handleChange} / >
109+ < button onClick= {handleClick}> Upload< / button>
110+ < / div>
111+ );
111112}
112113` ` `
113114</InlineFilter>
You can’t perform that action at this time.
0 commit comments