import React, { Component } from ‘react’;
import axios from ‘axios’;
class Abc extends Component {
constructor(props) {
super(props)
this.state = {
descriptions: []
}
}
componentDidMount() {
axios.get('https://jsonplaceholder.typicode.com/users')
.then(response => {
this.setState({ descriptions: response.data })
if (response.data) {
var rdata = response.data;
for (var r = 0; r < rdata.length; r++) {
if (r === 0) {
// console.log(rdata[r]);
// const {rdata}
this.dataEle = rdata[r]
console.log(this.dataEle.name)
}
}
}
})
.catch(error => {
console.log(error)
})
}
render() {
const { dataEle } = this.setState
return (
<div>
{dataEle.map((description, index) => (
<p key={index}>{description.description}</p>
))}
</div>
);
}
}
export default Abc;
const { dataEle } = this.setState
setState
is a function
Please tell me what is the error in my code
You’re not trying to map the state you need, you’re trying to map a function used to update the state. I assume there is a value in the state you want, you need to use
sorry for replying i just want display second object in the array…can you help on that, not all the objects
myArray[1]
. That isn’t particularly useful though because you need to know in advance the exact order of the array
myArray.find(element => {
// something to match the element you're
// looking for
return element === myCondition;
})
can you please edit in the code that i can understand…Please
const exampleArr = [
{ id: 1, title: "First item" },
{ id: 2, title: "Second item" },
{ id: 3, title: "Third item" },
];
// Finds the object with an `id` property of 2
exampleArr.find(element => element.id === 2)
// Finds the object with an `title` property of "Second item"
exampleArr.find(element => element.title === "Second item")
Please dont’mind
import React, { Component } from 'react';
import axios from 'axios';
class Abc extends Component {
constructor(props) {
super(props)
this.state = {
descriptions: []
}
}
componentDidMount() {
axios.get('https://jsonplaceholder.typicode.com/users')
.then(response => {
this.setState({ descriptions: response.data })
if (response.data) {
var rdata = response.data;
for (var r = 0; r < rdata.length; r++) {
if (r === 0) {
// console.log(rdata[r]);
// const {rdata}
this.dataEle = rdata[r]
console.log(this.dataEle.name)
}
}
}
})
.catch(error => {
console.log(error)
})
}
render() {
const { descriptions = [] } = this.state;
return (
<div>
{descriptions.map((description, index) => (
<p key={index}>{description.name}</p>
))}
</div>
);
}
}
export default Abc;
edit in this code. i am very fresher with React js…Please help me
i just want to display second object name… not all the object names…
Do you understand how to get an item at a specific index from an array? eg myArray[2]
would be the third item in myArray
.
map
is a function that takes an array and transforms each of the values in it some way, giving you a new array the same length as the original one. If you only want one specific item from an array, just get that item, don’t use map