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

156
Vistas
Probleme with slice in react js

Hello I have a problem with .slice

Error is : .slice is not a function

I want display customer commands's in a table but I have an error.

I get commands with my own API who return commands in json and axios convert it in a table

My Code :

{commands.slice(0, limit).map((command) => (
    <TableRow
        hover
        key={command._id}
    >
    <TableCell>
         {command.title}
    </TableCell>
    <TableCell>
         {moment(command.date).format('DD/MM/YYYY')}
    </TableCell>
            

Thank's in advance.

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

0

commands is probably not an array. You need to check and see if it is an array or not. To check whether commands is undefined or null you could add optional chaining like:

{commands?.slice(0, limit).map((command) => (...

or do something like:

{commands && commands.slice(0, limit).map((command) => (...

You can also check if the variable is an array or not:

{commands && Array.isArray(commands) && commands.slice(0, limit).map((command) => (...
about 4 years ago · Juan Pablo Isaza Denunciar

0

I have fix the problem.

The problem is which my state by default was a list and I have changed this to a table and it's worked!

My old code:

const [commands, setCommands] = useState({});
const [commands, setCommands] = useState([]);

I'm just an idiot 😅.

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