I have a React application and in it a component that displays data in a table. I am interested in displaying some things, including id, but not the whole thing, but only the first 5 characters. I try to display in the following way, and get an error id is not iterable
. What can be done to solve this?
const sliceId = (id) => {
const idCat = [...id].slice(0,5);
return idCat
}
in return:
clientList.map( (el, i) => (
<tr key={i} >
<td style={styleTd}>{ ...sliceId(el._id)}</td>
{ /* ... */ }
</tr>
))
2
Answers
For me in the end this is the code that worked:
The problem was that I had an initialState that was a different type and not a String. Thanks to you I solved, thank you.
The spread syntax (
...
) is not really necessary here, because as I suppose, theid
property is a string.I’d suggest you to get rid of it and just use the
String#slice
function but also secure the possibility thatid
is undefined using the optional chaining(?)
.