How do I reverse an array in react? I tried using this.state.data.maps.reverse but it ain't working. Thanks in advance!
import React, { Component } from 'react';
export default class Slideshows extends Component {
constructor(){
super();
this.state = {
data:[
{ "id": "1", "name": 'Robert', "age": "21" },
{ "id": "2", "name": 'Sam', "age": "33" },
{ "id": "3", "name": 'Jerry', "age": "42" },
]
}
}
render() {
const reverseData = this.state.map((data, i) => {
return (
<li> {data.name} | {data.age} </li>
)
})
return (
<ul>
{reverseData}
</ul>
)
}
}
You should get data
from your state first, then apply reverse()
on it, then map reversed array:
const reverseData = this.state.data.reverse().map((data, i) => {
return (
<li> {data.name} | {data.age} </li>
)
})
This question has already been addressed here, please visit this link below:
REACT Reverse an array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With