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.
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) => (...
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 😅.