42
loading...
This website collects cookies to deliver better user experience
npm install react-router-dom axios
App.jsx
:// @src/App.jsx
import React from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Home from "./Pages/Home";
import Album from "./Pages/Album";
const App = () => {
return (
<Router>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/album" component={Album} />
</Switch>
</Router>
);
};
export default App;
/album
route no parameters were defined, this is because the query strings are used as follows (in case you are not used to it):/album?id=56&artistId=7
Home.jsx
:// @src/Pages/Home.jsx
import React from "react";
const Home = () => {
return <h1>Main page</h1>
};
export default Home;
useState()
hook, as we know we're going to have a list of albums, the initial state will be an array.// @src/Pages/Home.jsx
import React, { useState } from "react";
const Home = () => {
const [albums, setAlbums] = useState([]);
return <h1>Main page</h1>
};
export default Home;
useEffect()
hook. We will also use axios to make the http request to the API and we will store the response data in our state.// @src/Pages/Home.jsx
import React, { useState, useEffect } from "react";
import axios from "axios";
const Home = () => {
const [albums, setAlbums] = useState([]);
useEffect(() => {
const fetch = async () => {
try {
const { data } = await axios.get("https://jsonplaceholder.typicode.com/albums");
setAlbums(data);
} catch (err) {
console.error(err);
}
};
fetch();
}, []);
return <h1>Main page</h1>
};
export default Home;
// @src/Pages/Home.jsx
import React, { useState, useEffect } from "react";
import axios from "axios";
const Home = () => {
const [albums, setAlbums] = useState([]);
useEffect(() => {
const fetch = async () => {
try {
const { data } = await axios.get("https://jsonplaceholder.typicode.com/albums");
setAlbums(data);
} catch (err) {
console.error(err);
}
};
fetch();
}, []);
return (
<article>
<code>Albums</code>
{albums.map((el) => (
<div key={el.id}>
<h2>{el.title}</h2>
</div>
))}
</article>
);
};
export default Home;
Link
component from the react router dom to redirect the user to the details page. However, we will have to add first to creating our query string before we create it. Like this:// @src/Pages/Home.jsx
import React, { useState, useEffect } from "react";
import { Link } from "react-router-dom";
import axios from "axios";
const Home = () => {
// Hidden for simplicity
return (
<article>
<code>Albums</code>
{albums.map((el) => (
<div key={el.id}>
<Link to={`/album?id=${el.id}&artistId=${el.userId}`}>
<h2>{el.title}</h2>
</Link>
</div>
))}
</article>
);
};
export default Home;
id
and the album artistId
(which corresponds to the userId).Album.jsx
:// @src/Pages/Album.jsx
import React from "react";
const Album = () => {
return <h1>Details page</h1>
};
export default Album;
// @src/Pages/Album.jsx
import React, { useState } from "react";
const Album = () => {
const [album, setAlbum] = useState({});
const [artist, setArtist] = useState({});
return <h1>Details page</h1>
};
export default Album;
useLocation()
hook. This hook returns an object that matches the current URL.// @src/Pages/Album.jsx
import React, { useState } from "react";
import { useLocation } from "react-router-dom";
const Album = () => {
const [album, setAlbum] = useState({});
const [artist, setArtist] = useState({});
const query = new URLSearchParams(useLocation().search);
const id = query.get("id");
const artistId = query.get("artistId");
return <h1>Details page</h1>
};
export default Album;
useEffect()
hook again to execute the http requests when the component is rendered.// @src/Pages/Album.jsx
import React, { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
const Album = () => {
const [album, setAlbum] = useState({});
const [artist, setArtist] = useState({});
const query = new URLSearchParams(useLocation().search);
const id = query.get("id");
const artistId = query.get("artistId");
useEffect(() => {
// Logic goes here
}, []);
return <h1>Details page</h1>
};
export default Album;
// @src/Pages/Album.jsx
import React, { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
const Album = () => {
const [album, setAlbum] = useState({});
const [artist, setArtist] = useState({});
const query = new URLSearchParams(useLocation().search);
const id = query.get("id");
const artistId = query.get("artistId");
useEffect(() => {
const fetch = async () => {
try {
// More logic goes here
} catch (err) {
console.error(err);
}
};
fetch();
}, []);
return <h1>Details page</h1>
};
export default Album;
// @src/Pages/Album.jsx
import React, { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
import axios from "axios";
const Album = () => {
const [album, setAlbum] = useState({});
const [artist, setArtist] = useState({});
const query = new URLSearchParams(useLocation().search);
const id = query.get("id");
const artistId = query.get("artistId");
useEffect(() => {
const fetch = async () => {
try {
const getAlbum = axios.get(`https://jsonplaceholder.typicode.com/albums/${id}`);
const getArtist = axios.get(`https://jsonplaceholder.typicode.com/users/${artistId}`);
// Even more logic goes here
} catch (err) {
console.error(err);
}
};
fetch();
}, []);
return <h1>Details page</h1>
};
export default Album;
.all()
method which will be used to execute both http requests at the same time. And let's pass the two http requests that we defined. Like this:// @src/Pages/Album.jsx
import React, { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
import axios from "axios";
const Album = () => {
const [album, setAlbum] = useState({});
const [artist, setArtist] = useState({});
const query = new URLSearchParams(useLocation().search);
const id = query.get("id");
const artistId = query.get("artistId");
useEffect(() => {
const fetch = async () => {
try {
const getAlbum = axios.get(`https://jsonplaceholder.typicode.com/albums/${id}`);
const getArtist = axios.get(`https://jsonplaceholder.typicode.com/users/${artistId}`);
const responses = await axios.all([getAlbum, getArtist]);
// Almost done
} catch (err) {
console.error(err);
}
};
fetch();
}, []);
return <h1>Details page</h1>
};
export default Album;
axios.all
will return to us is an array of promises and we just need to get the values of the responses from those same promises and store them in the respective states.// @src/Pages/Album.jsx
import React, { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
import axios from "axios";
const Album = () => {
const [album, setAlbum] = useState({});
const [artist, setArtist] = useState({});
const query = new URLSearchParams(useLocation().search);
const id = query.get("id");
const artistId = query.get("artistId");
useEffect(() => {
const fetch = async () => {
try {
const getAlbum = axios.get(`https://jsonplaceholder.typicode.com/albums/${id}`);
const getArtist = axios.get(`https://jsonplaceholder.typicode.com/users/${artistId}`);
const responses = await axios.all([getAlbum, getArtist]);
setAlbum(responses[0].data);
setArtist(responses[1].data);
} catch (err) {
console.error(err);
}
};
fetch();
}, []);
return <h1>Details page</h1>
};
export default Album;
// @src/Pages/Album.jsx
import React, { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
import axios from "axios";
const Album = () => {
// Hidden for simplicity
return (
<article>
<code>Song details</code>
<div>
<h1>{album.title}</h1>
<p>by: {artist.name}</p>
<button>Go back</button>
</div>
</article>
);
};
export default Album;
useHistory()
hook from the react router dom so that we can go back to the main page once the button is clicked.// @src/Pages/Album.jsx
import React, { useEffect, useState } from "react";
import { useHistory, useLocation } from "react-router-dom";
import axios from "axios";
const Album = () => {
const { push } = useHistory();
// Hidden for simplicity
return (
<article>
<code>Song details</code>
<div>
<h1>{album.title}</h1>
<p>by: {artist.name}</p>
<button onClick={() => push("/")}>Go back</button>
</div>
</article>
);
};
export default Album;