dev-resources.site
for different kinds of informations.
Cats Fatc's Quick project
Published at
4/20/2024
Categories
react
webdev
hooks
useeffect
Author
gurux3
Author
6 person written this
gurux3
open
Hello Dev's Yesterday i Uploded a Counter With Single button for both increment and decrement
*Today * i learned about useEffect littele bit more and it useFull tooo mee so much
by learned knowledge i code a simple random cats Facts website that fetch data from an Api
import { useEffect, useState } from "react";
import "./App.css";
import audio from "./assets/click-sound.mp3";
function App() {
const [catFact, setCatFact] = useState("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
const [clickSound, setClickSound] = useState(null);
useEffect(() => {
// Load the click sound
const sound = new Audio(audio);
setClickSound(sound);
}, []);
const playClickSound = () => {
if (clickSound) {
clickSound.play();
}
};
const fetchData = async () => {
try {
setLoading(true);
const response = await fetch("https://catfact.ninja/fact");
if (!response.ok) {
throw new Error("Unable to Fetch Data");
}
const data = await response.json();
setCatFact(data.fact);
setError(false);
playClickSound(); // Play sound when fetching data
} catch (err) {
setError(true);
} finally {
setLoading(false);
}
};
return (
<div className="App">
<h1>Cat Facts</h1>
<h1 className="Logo">ππΌπΏ</h1>
{loading && <h4 className="loading">Loading Data...β³</h4>}
{!loading && catFact && <h4>{catFact}</h4>}
{error && !loading && <h1 className="error">Error 404 ! π«</h1>}
{!loading && !catFact && !error && <h1>No facts available πΏ</h1>}
<button onClick={fetchData}>Show More Facts</button>
</div>
);
}
export default App;
link for the Site : https://catsfactsrandom.netlify.app/
Here Today i leared How use Api in React and Async and Await
Thank You. dev community
thank you....
hooks Article's
30 articles in total
Hooks Behind The Scenes 3, UseRef!!!
read article
A Complete Guide to All React Hooks for Beginners
read article
Using useReducer for Complex State Logic
read article
Descobrindo o useCallback do React
read article
Discovering Reactβs useCallback
read article
Mastering Code Quality in Laravel: Pint with Git Hooks and Docker
read article
React: leveraging custom hooks to extract reusable logic
read article
Hooks Behind The Scenes 2, useState!!
read article
Mastering React Functional Components: Hooks, useEffect, and Lifecycle Explained
read article
JSHooks API compare to CHooks
read article
The Ultimate Guide to Wall Hanging Hooks and Display Systems for Your Home and Office
read article
How to Detect if an Element is in View with React
read article
React useQuery : A Complete Guide
read article
{useState} HooK { Briefly Explained};
read article
React HooK= { Briefly Explained};
read article
What is useState?
read article
Actions - React 19
read article
React controlled and uncontrolled hooks
read article
React Concepts: Hook Proximity
read article
Demystifying React's useState Hook: A Comprehensive Guide
read article
Mastering React Hooks: Best Practices for Efficient and Maintainable Code
read article
Designing React Hooks for Flexibility
read article
Unleashing the Power of React Custom Hooks
read article
Understanding Context Hooks in React: A Beginner's Guide
read article
3 Ways To Create Engaging React Loading Screens with Hooks
read article
Understanding the useInteractionTimer Hook: Measuring Component Interaction Time in React
read article
Cats Fatc's Quick project
currently reading
All About Custom Hooks: Supercharge Your React Components
read article
Suggestion for new syntax of React reducers + context
read article
How To Define Typescript onChange Event In React
read article
Featured ones: