29
loading...
This website collects cookies to deliver better user experience
# Clone the repository
git clone https://github.com/crsandeep/simple-react-full-stack
# Go inside the directory
cd simple-react-full-stack
# Install dependencies
yarn (or npm install)
# Start development server
yarn dev (or npm run dev)
# Build for production
yarn build (or npm run build)
# Start production server
yarn start (or npm start)
api.openweathermap.org/data/2.5/box/city?bbox={bbox}&appid={API key}
$ npm install cors
$ npm install axios
$ npm install body-parser
index.js file
, we initialize these libraries:const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const axios = require('axios');
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('dist'));
app.get('/list', async (req, res) => {
try{
let result = await axios.get("http://api.openweathermap.org/data/2.5/box/city?bbox=12,32,15,37,10&appid=476787c5ccaf8b0949ff8b5bc02cdecc");
res.json(result.data);
} catch(e) {
console.log(e);
}
});
express.static(‘dist’)
to serve static files from the ‘dist’
directory.const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const axios = require('axios');
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('dist'));
app.get('/list', async (req, res) => {
try{
let result = await axios.get("http://api.openweathermap.org/data/2.5/box/city?bbox=12,32,15,37,10&appid=476787c5ccaf8b0949ff8b5bc02cdecc");
res.json(result.data);
} catch(e) {
console.log(e);
}
});
app.listen(process.env.PORT || 8080, () => console.log(`Listening on port ${process.env.PORT || 8080}!`));
npm install react-paginate --save
import axios from 'axios';
import ReactPaginate from 'react-paginate';
class App extends Component {
state = {
list: [],
perPage: 3,
page: 0,
pages: 0,
};
};
List
serves as an array that will store our data that will come from the server.perPage
is the number of rows that we will show in our table.Page
is the start page from which our table will begin.Pages
are how many pages are in the table initially.class App extends Component {
state = {
list: [],
perPage: 3,
page: 0,
pages: 0,
};
componentDidMount() {
this.makeHttpRequest();
}
makeHttpRequest = async() => {
let res = await axios.get('http://localhost:8080/list').catch(err => console.log(err));
const {perPage} = this.state;
const {list} = res.data;
this.setState({
list,
pages: Math.floor(list.length / perPage)
});
};
};
componentDidMount
.render() {
const {page, perPage, pages, list} = this.state;
let items = list.slice(page * perPage, (page + 1) * perPage);
let weathers = items.map(item => {
const { id, name, main } = item;
const { temp, humidity, pressure } = main;
const { speed } = item.wind;
return (
<tr key={id}>
<td>{name}</td>
<td>{temp}</td>
<td>{humidity}</td>
<td>{pressure}</td>
<td>{speed}</td>
</tr>
)
}) || '';
return (
<>
<table className='Table'>
<thead>
<tr>
<th>Name of the city</th>
<th>Temperature</th>
<th>Humidity</th>
<th>Pressure</th>
<th>Wind Speed</th>
</tr>
</thead>
<tbody>
{weathers}
</tbody>
</table>
</>
);
<tbody>
we put our variable weather, which will show our data. As a result, you should have the following table.<table>
tag.<ReactPaginate
previousLabel={'prev'}
nextLabel={'next'}
pageCount={pages}
onPageChange={this.handlePageClick}
containerClassName={'pagination'}
activeClassName={'active'}
/>
handlePageClick
method. It will look like this:handlePageClick = (event) => {
let page = event.selected;
this.setState({page})
}
App.js
file to get the big picture.import React, { Component } from 'react';
import './app.css';
import axios from 'axios';
import ReactPaginate from 'react-paginate';
class App extends Component {
state = {
list: [],
perPage: 3,
page: 0,
pages: 0,
};
componentDidMount() {
this.makeHttpRequest();
}
makeHttpRequest = async() => {
let res = await axios.get('http://localhost:8080/list').catch(err => console.log(err));
const {perPage} = this.state;
const {list} = res.data;
this.setState({
list,
pages: Math.floor(list.length / perPage)
});
};
handlePageClick = (event) => {
let page = event.selected;
this.setState({page})
}
render() {
const {page, perPage, pages, list} = this.state;
let items = list.slice(page * perPage, (page + 1) * perPage);
let weathers = items.map(item => {
const { id, name, main } = item;
const { temp, humidity, pressure } = main;
const { speed } = item.wind;
return (
<tr key={id}>
<td>{name}</td>
<td>{temp}</td>
<td>{humidity}</td>
<td>{pressure}</td>
<td>{speed}</td>
</tr>
)
}) || '';
return (
<>
<table className='Table'>
<thead>
<tr>
<th>Name of the city</th>
<th>Temperature</th>
<th>Humidity</th>
<th>Pressure</th>
<th>Wind Speed</th>
</tr>
</thead>
<tbody>
{weathers}
</tbody>
</table>
<ReactPaginate
previousLabel={'prev'}
nextLabel={'next'}
pageCount={pages}
onPageChange={this.handlePageClick}
containerClassName={'pagination'}
activeClassName={'active'}
/>
</>
);
}
}
export default App;
App.css
file in the /src
folder of your project and add the following styles to make the table look like in the screenshot:.Table {
border-spacing: 20px;
border: 1px solid #6c7ac9;
border-radius: 5px;
margin-top: 50px;
}
body {
margin: 0;
font-family: sans-serif;
color: #6c7ac9;
}
#root {
display: flex;
align-items: center;
flex-direction: column;
}
.pagination {
display: flex;
justify-content: space-between;
list-style: none;
margin-top: 20px;
padding: 0;
}
.pagination a {
cursor: default;
padding: 10px;
border-radius: 5px;
border: 1px solid #6c7ac9;
color: #6c7ac9;
margin-left: 10px;
}
.pagination li:not(.disabled) a:hover {
background-color: bisque;
cursor: pointer;
}
.pagination li a {
font-weight: bold;
}
.pagination li.active a {
color: #fff;
background: #6c7ac9;
}
.pagination li.disabled a {
pointer-events: none;
color: rgb(198, 197, 202);
border: 1px solid rgb(198, 197, 202);
}
import Pagination from '@material-ui/lab/Pagination'
and use <Pagination />
component.<Pagination />
components that you can customize for your needs. It also has a basic API for set upping a size of pagination and styles.