Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ ๐Ÿช Custom Hooks

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š ๐Ÿช Custom Hooks


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

"Clean code always looks like it was written by someone who cares." - Robert C. Martin

Why custom hooks?

  • Code Reusability: Enables reuse of stateful logic across multiple components, promoting DRY (Don't Repeat Yourself) principles.

  • Abstraction and Encapsulation: Abstracts away complex logic, providing a clean, encapsulated interface to components.Enhances code modularity and separation of concerns.

  • Simplified Components: Enables components to focus on UI concerns, leading to cleaner, more straightforward component code.

  • Testing: Allows for independent testing of custom hooks, ensuring the correctness of logic in isolation.

Now, I will validate why we should use custom hooks by going through real-world problems.

Problem: Write a function/component to get data from weather API. And display some prime weather data on the DOM.

Solution with Custom Hooks:

//app.js
import useWeatherData from "./useWeatherData";

const App = () => {
  const weatherData = useWeatherData("Rajshahi");
  const { temp = 0, humidity = 0, pressure = 0 } = weatherData?.main || {};

  return (
    <div>
      <h2>Temp: {Math.round(temp - 273.15)}โ„ƒ</h2>
      <h2>Humidity: {humidity}</h2>
      <h2>Pressure: {pressure}</h2>
    </div>
  );
};

root.render(<App />);

The code above ensures Reusability + Testing, Abstraction & Encapsulation.

  • โœ… Reusability: Suppose the same weather data is needed for another component, then we can use it anywhere. This doesn't need to re-create. Once you create the hook, you can use it anywhere you want.

  • โœ… Testing: If app testing requires the create separate test cases on a particular API get function, it's widely possible to make individual test cases for specific tasks.

  • โœ… Abstraction & Encapsulation: As you can see, the code is much cleaner and easier to read in one go. Custom hooks can illuminate code complexity and redundancy and make it easy to develop.

Solution without Custom Hooks:

//app.js
import ReactDOM from "react-dom/client";
import { useEffect, useState } from "react";
const API_KEY = "08373cd61534f123f9890109d221671b";
const cityName = "Rajshahi";
const App = () => {
  const [weatherData, setWeatherData] = useState({});

  useEffect(() => {
    const getData = async () => {
      const data = await fetch(
        `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}`
      );
      const json = await data.json();
      setWeatherData(json);
    };
    getData();
  }, []);

  return (
    <div>
      <h2>Temp: {Math.round(weatherData?.main?.temp - 273.15)}โ„ƒ</h2>
      <h2>Humidity: {weatherData?.main?.humidity}</h2>
      <h2>Pressure: {weatherData?.main?.pressure}</h2>
    </div>
  );
};

const root = ReactDOM.createRoot(document.getElementById("root"));

root.render(<App />);

I must say this is not cleaner anyway with time. When the apps tend to gain complex logic, it will be tough to debug and maintain. Considering these concepts like testing, debugging, and developing, the best practice would be using the custom hook.

...



๐Ÿ“Œ Bloodline of Hooks: Custom Hooks in React for Advanced Devs


๐Ÿ“ˆ 41.75 Punkte

๐Ÿ“Œ Flutter Hooks Tutorial: Flutter Animation Using Hooks (useEffect and useAnimationController)


๐Ÿ“ˆ 32.94 Punkte

๐Ÿ“Œ React Hooks โ€“ How to Use the useState & useEffect Hooks in Your Project


๐Ÿ“ˆ 32.94 Punkte

๐Ÿ“Œ React Hooks โ€“ How to Use the useState & useEffect Hooks in Your Project


๐Ÿ“ˆ 32.94 Punkte

๐Ÿ“Œ Writing Custom React Hooks with Typescript


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Custom Hooks made easy :)


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Creating Custom Hooks in React and TypeScript


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ 15 Useful React Custom Hooks That You Can Use In Any Project


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ A Beginner's Guide to React Custom Hooks


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Creating Custom Hooks in React: Enhancing Component Reusability with Audio Playback


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ A Guide to React Custom Hooks


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Data Pagination in React (Nextjs) with Custom Hooks


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Demystifying Custom Hooks in React


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ ๐Ÿช Custom Hooks


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Understanding State Management in React: Avoiding Pitfalls with Custom Hooks


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ A Comprehensive Guide to Writing Custom Hooks โœจ"


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ All About Custom Hooks: Supercharge Your React Components


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ React Custom Hooks (useFluidFont)


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Unleashing the Power of React Custom Hooks


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ React Custom Hooks (useCounter)


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Using Custom Hooks in multiple React Components.


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Using Custom React hooks in NextJS?


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ React Custom Hooks (useLocal)


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ React Custom Hooks (useRefs)


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ React Custom Hooks (useEventListener)


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Top 10 Most Useful Custom Hooks in React


๐Ÿ“ˆ 25.28 Punkte

๐Ÿ“Œ Bugtraq: Admin Custom Login WordPress plugin custom login page affected by persistent Cross-Site Scripting


๐Ÿ“ˆ 17.61 Punkte

๐Ÿ“Œ WordPress Absolutely Glamorous Custom Admin ag-custom-admin Plugin Database Backup Arbitrary File Download Vulnerability


๐Ÿ“ˆ 17.61 Punkte

๐Ÿ“Œ [Custom Linux ISO] An Arch Linux Based Custom Live ISO For Everyone #lovelinux


๐Ÿ“ˆ 17.61 Punkte

๐Ÿ“Œ [Custom Linux ISO] An Arch Linux Based Custom Live ISO For Everyone #lovelinux


๐Ÿ“ˆ 17.61 Punkte

๐Ÿ“Œ Low CVE-2019-14789: Custom 404 pro project Custom 404 pro


๐Ÿ“ˆ 17.61 Punkte

๐Ÿ“Œ Low CVE-2019-15838: Custom 404 pro project Custom 404 pro


๐Ÿ“ˆ 17.61 Punkte











matomo