Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda