This is my solution to the 'Advice generator app' challenge on Frontend Mentor. Frontend Mentor challenges help me improve my coding skills by building realistic projects.
Users should be able to:
- View the optimal layout for the site depending on their device's screen size
- See hover states for all interactive elements on the page
- Generate a new piece of advice by clicking the dice icon
- Solution URL: View the GitHub repository here
- Live Site URL: I have hosted the project on GitHub Pages here
- Semantic HTML5 markup
- CSS custom properties
- Flexbox - CSS web layout model
- TypeScript - Programming language that extends JavaScript
- React - JavaScript library
- Node.js - JavaScript runtime environment
- Vite - React build tool with local development server
- Advice Slip JSON API - API that generates random advice
- I learned how to consume
APIs
. After reading the documentation, I used thefetch()
function to makeHTTP requests
to the Advice Slip JSON API endpoints. On receiving response, data is processed to extract the advice and ID used to update thestate
of the app. I also wrote code tohandle errors
.- I also learned that I could pass the
function
that makes theAPI call
and updates the state to thebutton
asprops
so that the button can be used to trigger the state change. In my case, thegetAdvice
function.
- I also learned that I could pass the
import { useState } from "react";
import "./App.css";
import AdviceCard from "./components/AdviceCard";
import Button from "./components/Button";
import PatternDividerDesktop from "./components/PatternDividerDesktop";
import PatternDividerMobile from "./components/PatternDividerMobile";
const App = () => {
const [id, setId] = useState(117);
const [advice, setAdvice] = useState(
"It is easy to sit up and take notice, what's difficult is getting up and taking action."
);
const getAdvice = () => {
fetch("https://api.adviceslip.com/advice")
.then((response) => response.json())
.then((data) => {
const newId = data.slip.id;
setId(newId);
const newAdvice = data.slip.advice;
setAdvice(newAdvice);
console.log(newId, newAdvice);
})
.catch((error) => {
console.log("Error fetching advice:", error);
});
};
return (
<div className="app">
<main>
<div className="card-container">
<AdviceCard adviceId={id}>{advice}</AdviceCard>
<div className="pattern-divider-desktop">
<PatternDividerDesktop />
</div>
<div className="pattern-divider-mobile">
<PatternDividerMobile />
</div>
<div className="button-container">
<Button onclick={getAdvice} />
</div>
</div>
</main>
</div>
);
};
export default App;
- I learned that
letter-spacing
in CSS can be used to set the space between letters in a text. Before now, I thought it was fixed and dependent on thefont-family
.
- Consuming APIs
- CSS Flexbox
- Media queries
- Typescript
- React
I found these techniques very useful. I will continue focusing on them in future projects to refine and perfect them.
- useContext In 2 Minutes - the React Hooks series - This YouTube video helped me understand
useContext
and when to use it. I'd recommend it to anyone still learning this concept.
- GitHub - @ArinzeGit
- Frontend Mentor - @ArinzeGit
- LinkedIn - @Dennings-Owoh
- Instagram - @_.arinze._
- Twitter - @Arinze98433402