Answers for "set value file input react"

0

set value file input react

import React, { useEffect, useRef } from "react";

const FileInput = ({ value, ...rest }) => {
  const inputRef = useRef();
  
  useEffect(() => {
    if (value === "") {
      inputRef.current.value = "";
    } else {
      inputRef.current.files = value;
    }
  }, [value]);
  
  return <input {...rest} type="file" ref={inputRef} />;
};

export default FileInput;
Posted by: Guest on October-20-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language