React - conditional rendering text

0 votes
68 views
added Feb 15 in React by lcjr First Warrant Officer (11,850 points)

conditionally rendering the text based on whether a file has been selected or not. Here's how you can update your code to display "Maximum upload file size: 100MB" by default and switch to "Selected file: {fileName}" once a file is chosen:

const CSVUploader = ({ onUpload }) => {
  const [file, setFile] = useState(null);
  const [fileName, setFileName] = useState('');
  const router = useRouter();

  const handleFileChange = e => {
    const selectedFile = e.target.files[0];
    setFile(selectedFile);
    setFileName(selectedFile ? selectedFile.name : ''); // Set filename if file is selected
  };
  
  return (
    <div>
      // Display filename if selected
      {file ? ( 
        <p
          className="selected-file"
          css={css`
            margin-top: 5px;
            font-size: 12px;
            color: #888;
          `}
        >
          Selected file: {fileName}
        </p>
      ) : (
        <p
          className="upload-info"
          css={css`
            margin-top: 5px;
            font-size: 12px;
            color: #888;
          `}
        >
          Maximum upload file size: 100MB
        </p>
      )}
    </div>
  );
};

const errorMessage = css`
  color: red;
`;

export default CSVUploader;

 

1 Response

0 votes
responded Feb 15 by lcjr First Warrant Officer (11,850 points)

When click the button, shows 'Uploading'. by default shows 'Upload CSV'

<button
  css={css`
    background-color: #2056ab;
    border-radius: 4px;
    font-size: 20px; 

    &:hover {
      background-color: #1d4481;
    }
  `}
  onClick={handleUpload}
  disabled={loading}
>
  {loading ? 'Uploading...' : 'Upload CSV'}
</button>

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...