Upload Files in React - Typescript, Drag and Drop, & Form Examples

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ค. 2023
  • Learn how to upload files like images from a form with drag and drop in a React app.
    We'll walk through setting up an HTML filepicker input to select files, read the files and show a preview using the Filereader API, uploading them to services like Cloudinary, configuring the file input to accept only certain types, and seeing how we can easily add drag and drop with React Dropzone.
    Dig deeper into Full Stack React with my upcoming Appwrite course: spacejelly.dev/reactappwrite
    🧰 Resources
    Tutorial: spacejelly.dev/posts/uploadin...
    Starter: github.com/colbyfayock/demo-r...
    Code: github.com/colbyfayock/my-rea...
    Demo: my-react-file-upload.vercel.app/
    react-dropzone.js.org/
    📺 TH-cam: th-cam.com/users/colbyfayock?sub_c...
    🐦 Twitter: / colbyfayock
    📹 Twitch: / colbyfayock
    ✉️ Newsletter: www.colbyfayock.com/newsletter/
    🎥 What I Use: www.colbyfayock.com/uses
    #colbyfayock #reactjs #uploadfile #draganddrop #webdevelopment
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 48

  • @colbyfayock
    @colbyfayock  4 วันที่ผ่านมา

    Learn how to build a full stack Next.js app in my upcoming course: colbyfayock.com/course

  • @tastymuffinmm
    @tastymuffinmm 11 หลายเดือนก่อน +7

    Super helpful tutorial, straight to the point and good examples. Subscribed and looking forward to more!

    • @colbyfayock
      @colbyfayock  11 หลายเดือนก่อน

      thank you!! 🙌

  • @Brlitzkreig
    @Brlitzkreig 3 หลายเดือนก่อน +1

    Wow, you're an excellent teacher you explain it so concisely

    • @colbyfayock
      @colbyfayock  3 หลายเดือนก่อน

      thank you🙏

  • @PickleRiiiiiiick
    @PickleRiiiiiiick 9 หลายเดือนก่อน +1

    Your tutorials are always solid and getting better. Thank you sir.

    • @colbyfayock
      @colbyfayock  9 หลายเดือนก่อน

      appreciate the kind words!

  • @jennysukut4346
    @jennysukut4346 19 วันที่ผ่านมา

    Fantastic! What a helpful + concise tutorial. Thanks for sharing!

    • @colbyfayock
      @colbyfayock  12 วันที่ผ่านมา

      glad to hear it helped! 🙌

  • @andinuruljihad2912
    @andinuruljihad2912 10 หลายเดือนก่อน +1

    great tutorial. i will watch your youtube career with great interest.

    • @colbyfayock
      @colbyfayock  10 หลายเดือนก่อน

      thank you!

  • @peapuffer3514
    @peapuffer3514 ปีที่แล้ว +1

    What's up buddy.😊 Watching your video after a while. Keep it up

  • @yourlinuxguy
    @yourlinuxguy 4 หลายเดือนก่อน

    Amazing tutorial, Thankyou so much.

    • @colbyfayock
      @colbyfayock  4 หลายเดือนก่อน

      thanks and no problem!

  • @leonardozerino9628
    @leonardozerino9628 4 หลายเดือนก่อน

    Great video my dude, thank you so much.

    • @colbyfayock
      @colbyfayock  4 หลายเดือนก่อน

      no problem!

  • @raminkhodaie4063
    @raminkhodaie4063 9 หลายเดือนก่อน

    very insightful video

    • @colbyfayock
      @colbyfayock  8 หลายเดือนก่อน

      glad it was helpful!

  • @techcoderuz
    @techcoderuz 3 หลายเดือนก่อน +1

    thanks. Great tutorial

  • @shooting4star2023
    @shooting4star2023 7 วันที่ผ่านมา

    I believe that it's slightly better to use ref to hold the file, instead of state. As the selected file doesn't affect the look of the page and a rerender (checking on the V-DOM to be technically accurate) is not necessary with the state.

    • @colbyfayock
      @colbyfayock  5 วันที่ผ่านมา

      would you imagine then that the file would be stored in a ref but then a reference to the file would be stored in state? the existence of the file does impact the look of the page as we display the preview so we need a mechanism to be able to hook into the render lifecycle

  • @blank.haruka
    @blank.haruka 7 หลายเดือนก่อน

    Thank you sm!

    • @colbyfayock
      @colbyfayock  7 หลายเดือนก่อน

      no problem!

  • @davidsehi7109
    @davidsehi7109 ปีที่แล้ว

    Thanks man

  • @John-eq5cd
    @John-eq5cd 6 หลายเดือนก่อน

    Another clear and useful video, thanks!
    A couple of questions, though...
    Do we need our Cloudinary api_key to upload assets? I can successfully do this using the Cloudinary url and referencing my cloud name in the url, nothing else needed.
    Secondly, by using NEXT_PUBLIC_ in front of your environment variable as you suggest below, is this not exposing that env variable?

    • @colbyfayock
      @colbyfayock  6 หลายเดือนก่อน +1

      you need the API key unless you're doing an unsigned upload which is defined using an Upload Presset
      yup, that exposes it to the client, but the API Key is okay for this use, you do _not_ want to expose the API Secret

  • @abderrahmaneelfilali5933
    @abderrahmaneelfilali5933 8 หลายเดือนก่อน

    suuper helpful

  • @ruairidhgrass3479
    @ruairidhgrass3479 11 หลายเดือนก่อน

    Thanks man, how did you get an environment variable in a client component? It's giving me an error,

    • @colbyfayock
      @colbyfayock  11 หลายเดือนก่อน

      no problem! you prepend VITE_ if youre using React with Vite to make it public: github.com/colbyfayock/my-react-file-upload/blob/main/src/pages/contact.tsx#L42

    • @ruairidhgrass3479
      @ruairidhgrass3479 11 หลายเดือนก่อน

      @@colbyfayock thanks for the quick reply man, sorry I should have specified. I’m using nextjs on my project

    • @colbyfayock
      @colbyfayock  11 หลายเดือนก่อน

      @@ruairidhgrass3479np, similarly you'd want to add NEXT_PUBLIC_ in front of your environment variable then

  • @vikashmishra7160
    @vikashmishra7160 7 หลายเดือนก่อน +1

    @8:55 src={preview} was showing error .. how did you resolve it?

    • @colbyfayock
      @colbyfayock  7 หลายเดือนก่อน

      added src={preview as string} github.com/colbyfayock/my-react-file-upload/blob/main/src/pages/contact.tsx#L90

  • @PelleWidell
    @PelleWidell 7 หลายเดือนก่อน

    Very good video. But what if I want to paste a snippet from memory?

    • @colbyfayock
      @colbyfayock  7 หลายเดือนก่อน

      hey thanks! good question, did a quick lookup and this codepen seems to work, check the web console upon paste: codepen.io/appsoftware/pen/WNwWpzW
      this would make for a good tutorial! will add it to my list

  • @crypto1886
    @crypto1886 4 หลายเดือนก่อน +1

    Hey dude great video, I am a newbie in react and javascript and am running into an error in this line of code:
    const { acceptedFiles, getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop });
    Error:
    Type '(acceptedFiles: FileList) => void' is not assignable to type '(acceptedFiles: T[], fileRejections: FileRejection[], event: DropEvent) => void'.
    Types of parameters 'acceptedFiles' and 'acceptedFiles' are incompatible.
    Property 'item' is missing in type 'T[]' but required in type 'FileList'.ts(2322)
    lib.dom.d.ts(8330, 5): 'item' is declared here.
    Any clue how to fix this?

    • @colbyfayock
      @colbyfayock  4 หลายเดือนก่อน

      hey have you seen my code for how i set it up? github.com/colbyfayock/my-react-file-upload/blob/main/src/pages/contact.tsx
      i just spun it up and dont see any errors with that, perhaps its an issue with how you have onDrop set up, where i see FileList, are you using FileList instead of Array perhaps?

  • @OzzTheWizzard
    @OzzTheWizzard 8 หลายเดือนก่อน +1

    can you make a video showing how to do this one by one, step by step from scratch?

    • @salijoghli
      @salijoghli 7 หลายเดือนก่อน +1

      Bro he literally did that

    • @OzzTheWizzard
      @OzzTheWizzard 7 หลายเดือนก่อน

      @@salijoghli let me rewatch it and comment again

  • @shreyaishere6783
    @shreyaishere6783 8 หลายเดือนก่อน

    If we want a pdf or doc file instead of image?

    • @colbyfayock
      @colbyfayock  8 หลายเดือนก่อน

      have you tried? it should work the same as long as you're not restricting the filetype

  • @andelianzio7457
    @andelianzio7457 หลายเดือนก่อน

    And what if a I wanna upload the image + data (name, email, message)

    • @colbyfayock
      @colbyfayock  หลายเดือนก่อน

      typically you'll store that kind of data in a database. some databases come with file storage that you could also use in situations like that, but often they dont which leaves you to need a service like Cloudinary or S3, where you would then store the ID / URL in the database alongside the rest