i got this error while building an app index.js:1375 Warning: Each child in a list should have a unique "key" prop. in App (at src/index.js:7) this the response from the api , i have this problem also i cant render the array as list properly
(15) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
here is my code
import React, { Component } from 'react'
import ImagesSearch from './ImagesSearch'
import axios from 'axios'
export class Images extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
searchquery: '',
};
this.onChange = this.onChange.bind(this)
this.onSubmit = this.onSubmit.bind(this)
}
onChange(e) {
this.setState({searchquery:e.target.value})
}
onSubmit(e) {
e.preventDefault()
this.images(this.state.searchquery)
console.log(this.state.searchquery)
}
images(query) {
return axios.get(`https://api.pexels.com/v1/search?
query=${query}+query&per_page=15&page=1`, {
headers: { Authorization: `` }
}).then(res => {
console.log(res.data.photos)
this.setState(prevState => ({
data: [res.data.photos]
}))
console.log(this.state.data)
return res.data
}).catch(err => {
console.log(err)
})
}
render() {
const mapRows = this.state.data.map((item) => (
<div key={item.id}>
<li>
<span>Name : {item.url}</span>
<span>User Type: {item.url}</span>
</li>
</div>))
return (
<div>
<form class="form-group m-2" onSubmit={this.onSubmit}>
<label>Search</label>
<input class="form-control form-control-sm w-25 mx-auto" name="searchquery" type="text" placeholder="Search" onChange={this.onChange} />
<button type="submit" class="btn btn-primary mt-2">Submit</button>
</form>
<div>
{ mapRows }
<div>
</div>
)
}
}
export default Images
thank you for your help
setStatephase is wrong and that's why you can't render. I updated my answer. Please check my answer what you did wrong.