Using the create-react-app, I have created a simple React app to display the data fetched from the Flask endpoint. Though the fetch function can fetch the data from the HTTP endpoint, the map function can not read the retrieved data. The app produces errors like
TypeError: Cannot read property 'dataTime' of undefined (anonymous function) C:/Users/정보기획팀/Dropbox/yu_project/yu_blog_project/air_quality_react/src/DataList.js:10 7 | return( 8 | <div> 9 | {data.map((datum, i) => { > 10 | return <Data key={i} time={datum[i].dataTime} pm10={datum[i].pm10Value} pm25={datum[i].pm25Value} />; | ^ 11 | })} 12 | </div> 13 | ) The App.js is
import React, {Component} from 'react'; import './App.css'; import DataList from './DataList'; class App extends Component { constructor(props) { super(props); this.state = { airData: [] }; } componentDidMount() { fetch('http://210.123.33.247/airData') .then(response => response.json()) .then(data => this.setState({ airData: data }) ); } render() { const {airData} = this.state; console.log('air data from the state ', airData); return ( <div className="App"> <p>Data from the air quality</p> <DataList data={this.state.airData} /> </div> ); } } export default App; And the DataList.js is
import React from 'react'; import Data from './Data'; const DataList = ({data}) => { console.log('from DataList', data[0]); console.log('pm value', data[3]); return ( <div> {data.map((datum, i) => { return ( <Data key={i} time={datum[i].dataTime} pm10={datum[i].pm10Value} pm25={datum[i].pm25Value} /> ); })} </div> ); }; export default DataList; And the Data.js is
import React from 'react'; const Data = ({time, pm10, pm25}) => { return ( <div> <p>{pm10}</p> <p>{pm25}</p> <p>{time}</p> </div> ) } export default Data The index.js is
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import * as serviceWorker from './serviceWorker'; ReactDOM.render(<App />, document.getElementById('root')); serviceWorker.unregister(); I appreciate it if someone please help me to locate the error. Many thanks.