I want to learn how to display the data from the API in reactjs..
This is the API and I want to display description
property in the API whichis in weather[0]
object and temp
property which is in main
object.
How to do it ?
This the code:
import logo from './Images/logo.png';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Navbar, Button, Form, FormControl } from 'react-bootstrap';
import React, { Component } from "react";
class App extends Component {
constructor(props) {
super(props);
this.state = {};
}
componentDidMount() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(async function (position) {
console.log("Latitude is :", position.coords.latitude);
console.log("Longitude is :", position.coords.longitude);
var lat = position.coords.latitude;
var lon = position.coords.longitude;
const url = `http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=metric&appid=ca148f5dc67f12aafaa56d1878bb6db2`;
const response = await fetch(url);
let data = await response.json();
console.log(data);
});
}
}
render() {
return (
<div className="App">
<Navbar bg="light" expand="lg">
<Navbar.Brand href="#">Weather Forecast <img src={logo} alt="logo" width="50" height="50" /></Navbar.Brand>
<Navbar.Toggle aria-controls="navbarScroll" />
<Navbar.Collapse id="navbarScroll">
<Form className="d-flex">
<FormControl
type="search"
placeholder="Enter City"
className="mr-2"
aria-label="Search"
/>
<Button variant="outline-success" className="searchBTN">Forecast</Button>
</Form>
</Navbar.Collapse>
</Navbar>
</div>
);
}
}
export default App;
Can I get some example how to display these properties on the page ?
SOLVED
Just replace async function (position)
in navigator.geoposition
function with async (position) =>
and the data come sucsessfully.
Comments
Post a Comment