Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

327
Vistas
When using useState with an array argument, how do you use useEffect to update a button dynamically?

New to React so be gentle. I created a file uploader component and I am adding a button in App.js that is dependent on the state of the files uploaded. If there are no files uploaded, it will say "No files uploaded". If there are files uploaded it will say "Create new project". I'm using the useState hook to set my Project info:

const [newProjectInfo, setNewProjectInfo] = useState([documents: []});
import React, { useEffect, useRef, useState } from "react";
import FileUpload from "./components/file-upload/fileUpload";
import {
  //Custom components created with styled-components
  UploadToProjectContainer,
  CreateNewProjectBtn,
  NoFilesSelectedBtn
} from "./components/file-upload/fileUploadStyles";

function App() {

  const [newProjectInfo, setNewProjectInfo] = useState({
    documents: []
  });

  const updateUploadedFiles = (files) =>
    setNewProjectInfo({ ...newProjectInfo, documents: files});

  const handleSubmit = (event) => {
    event.preventDefault();
    //add logic to create a new project
  }

  return (
    <div>
      <form
        onSubmit={handleSubmit}
      >
        <FileUpload
          accept=".jpg,.png,.jpeg,.pdf,.doc,.gif,.bmp,.tiff,.svg,.dwg,.docx,.eps,.exe"
          label="File(s)"
          multiple
          updateFilesCb={updateUploadedFiles}
        />
        <UploadToProjectContainer>
          <button>
            Create New Project.
          </button>
        </UploadToProjectContainer>

      </form>
    </div>
  );

}


export default App;

As you can see, right now there is a static "Create New Project" button. I created styled containers for each of the buttons I want to render dynamically (see: CreateNewProjectBtn & NoFilesSelectedBtn), but I cannot access the props of the state to change the button when a file is uploaded. I know I have to use useEffect but the document[] prop of newProjectInfo is inaccessible - but that is what is being updated. Hopefully this was clear. Please let me know if I need to clarify.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I see, you want to conditionally render different button text based on the newProjectInfo.documents array being populated or not. Use a ternary operator to check if the newProjectInfo.documents array has a truthy length (non-zero) property to conditionally render one string or the other.

No need to use an useEffect for this, updating the state will trigger a rerender. Just compute the displayed button text per render cycle.

Example:

<button>
  {newProjectInfo.documents.length ? "No files uploaded" : "Create New Project"}
</button>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda