This is my App.js file. I am trying to fetch data using fetch()
What am I missing? Getting Error as TypeError: Cannot read property
map of undefined". What is wrong? How do I get the result?
I'm following the reactjs tutorial, and I keep running into an issue
when passing the value from the state of one component into another
component. The error 'Cannot read property 'map' of undefined' is thrown
when the map function in the CommentList component is executed. What
would cause the prop to become undefined when fetching data? I also
tried putting debugger, why it is not going inside the fetch call?
import React, { Component } from 'react';
import UsingFetch from './UsingFetch';
class App extends Component {
constructor(props){
super(props);
this.state = {
companyList:[]
};
}
componentDidMount(){
fetch('https://jsonplaceholder.typicode.com/users')
.then(res => { res.json() })
.then(
res => {
this.setState({
companyList : res
});
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
render() {
return (
<div className="App">
1) DataGrid
<UsingFetch datasource = {this.state.companyList}/>
</div>
);
}
}
export default App;
This is my UsingFetch.js File
class UsingFetch extends Component{
constructor(){
super();
this.state = {
isLoaded:false,
}
}
render() {
return (
<div>
{this.props.datasource.map(item =>
<div>
{item.name}
</div>
)}
</div>
)
}
}
This is my App.js and UsingFetch.js file. I am trying to fetch data using fetch(), What am I missing? Getting Error as:
TypeError: Cannot read property map of undefined"
What is wrong? How do I get the result?