Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!