Logo

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
Categories
4 categories in total
react
open
webdev
open
hooks
open
useeffect
open
Author
6 person written this
gurux3
open
Cats Fatc's Quick project

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;


Enter fullscreen mode Exit fullscreen mode

Image description

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
Favicon
Hooks Behind The Scenes 3, UseRef!!!
Favicon
A Complete Guide to All React Hooks for Beginners
Favicon
Using useReducer for Complex State Logic
Favicon
Descobrindo o useCallback do React
Favicon
Discovering React’s useCallback
Favicon
Mastering Code Quality in Laravel: Pint with Git Hooks and Docker
Favicon
React: leveraging custom hooks to extract reusable logic
Favicon
Hooks Behind The Scenes 2, useState!!
Favicon
Mastering React Functional Components: Hooks, useEffect, and Lifecycle Explained
Favicon
JSHooks API compare to CHooks
Favicon
The Ultimate Guide to Wall Hanging Hooks and Display Systems for Your Home and Office
Favicon
How to Detect if an Element is in View with React
Favicon
React useQuery : A Complete Guide
Favicon
{useState} HooK { Briefly Explained};
Favicon
React HooK= { Briefly Explained};
Favicon
What is useState?
Favicon
Actions - React 19
Favicon
React controlled and uncontrolled hooks
Favicon
React Concepts: Hook Proximity
Favicon
Demystifying React's useState Hook: A Comprehensive Guide
Favicon
Mastering React Hooks: Best Practices for Efficient and Maintainable Code
Favicon
Designing React Hooks for Flexibility
Favicon
Unleashing the Power of React Custom Hooks
Favicon
Understanding Context Hooks in React: A Beginner's Guide
Favicon
3 Ways To Create Engaging React Loading Screens with Hooks
Favicon
Understanding the useInteractionTimer Hook: Measuring Component Interaction Time in React
Favicon
Cats Fatc's Quick project
Favicon
All About Custom Hooks: Supercharge Your React Components
Favicon
Suggestion for new syntax of React reducers + context
Favicon
How To Define Typescript onChange Event In React

Featured ones: