Course Content
Subtitle Guide – Hướng dẫn thêm phụ đề
0/1
02 – Optional React Refresher
0/77
03 – NextJS Essentials (App Router)
0/49
15 – Optional NextJS Summary
0/37
16 – Course Roundup
0/1
Next.js 14 & React – The Complete Guide
About Lesson

There are two improvements you could / should make to that “Image Picker” component:

1) Reset the previewed image if no image was selected:

Add set setPickedImage(null); to the if (!file) block:

if (!file) {
  setPickedImage(null);
  return;
}

2) Add the required prop to the (hidden) <input> element:

<input
  className={classes.input}
  type="file"
  id={name}
  accept="image/png, image/jpeg"
  name={name}
  ref={imageInput}
  onChange={handleImageChange}
  required
/>

This ensures that the <form> can’t be submitted without an image being selected.

Exercise Files
034 Improving the Image Picker Component.zip
Size: 593.00 B
Join the conversation