Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ How to Create your Own useWindowSize Hook in ReactJS

๐Ÿ  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



๐Ÿ“š How to Create your Own useWindowSize Hook in ReactJS


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

Introduction:

As a ReactJS developer, you may encounter situations where you need to dynamically show or hide a component or element based on the browser's window dimensions. Normally, CSS Media Queries are used for this purpose, but if you are using ReactJS, you can also use the useWindowSize hook to dynamically render a component or element. Therefore, in this tutorial, I will guide you through the process of creating your custom useWindowSize hook.

Approach:

We will conditionally render the div named medium when the screen width is greater than 750px, and we will also render the div named large when the screen width is greater than 1024px.

App.js

// global stylesheet
import "./styles.css";

export default function App() {
  return (
    <div className="container">
      <div className="box">Small</div>
      <div className="box">Medium</div>
      <div className="box">Large</div>
    </div>
  );
}

In the above App.js component, we have a parent div with the class container and three child elements with the class box.

styles.css

.container {
  display: flex;
  justify-content: center;
  gap: 2rem;
}

.box {
  background-color: coral;
  padding: 2rem;
  color: white;
  font-weight: bold;
  border-radius: 1rem;
}

We added some styles to the "container" class and "box" class in the global stylesheet to improve their appearance.

useWindowSize Hook

It's time to create our useWindowSize hook. To do so, navigate to the src folder, and if you haven't already, create a hooks folder within it. Next, create a file named useWindowSize.js.

useWindowSize.js

import { useState, useEffect } from "react";

function useWindowSize() {
  // Initial screen width and height
  const [screenWidth, setScreenWidth] = useState(window.innerWidth);
  const [screenHeight, setScreenHeight] = useState(window.innerHeight);

  // Will run after mount
  useEffect(() => {
    // function to update screenWidth & screenHeight states
    function handleResize() {
      setScreenWidth(window.innerWidth);
      setScreenHeight(window.innerHeight);
    }

    // listen to resize event and update states
    window.addEventListener("resize", handleResize);

    // cleanup function to safely remove eventListener
    return () => window.removeEventListener("resize", handleResize);
  }, []);

  return { screenWidth, screenHeight };
}

export default useWindowSize;

We've initialized the screenWidth and screenHeight states with the height and width of the browser viewport. Within the useEffect hook, we've defined a function named handleResize, which updates the screenWidth and screenHeight states whenever the user resizes the browser window. To enable this functionality, we've added an event listener that listens to the resize event and calls the handleResize function to update the states accordingly. Finally, we've implemented a cleanup function that safely removes the event listener.

Using useWindowSize Hook

Now that our useWindowSize hook is ready to use, let's import it into the App.js component and use it to conditionally render the medium and large named divs.

App.js

// global stylesheet
import "./styles.css";

// hook
import useWindowSize from "./hooks/useWindowSize";

export default function App() {
  // Using screenWidth state from useWindowSize hook
  const { screenWidth } = useWindowSize();

  return (
    <div className="container">
      <div className="box">Small</div>

      {/* Conditionally showing Medium named div */}
      {screenWidth > 750 ? <div className="box">Medium</div> : null}

      {/* Conditionally showing Large named div */}
      {screenWidth > 1024 ? <div className="box">Large</div> : null}
    </div>
  );
}

I hope this has been helpful and informative for you. If you have any questions, comments, or feedback, feel free to leave a comment below! Happy coding! โค

...



๐Ÿ“Œ How to Create your Own useWindowSize Hook in ReactJS


๐Ÿ“ˆ 88.37 Punkte

๐Ÿ“Œ Writing a Custom useWindowSize React Hook


๐Ÿ“ˆ 51.18 Punkte

๐Ÿ“Œ How to create your own useContext hook in vanilla js?


๐Ÿ“ˆ 35.56 Punkte

๐Ÿ“Œ Commonly asked ReactJS interview questions. Here are ReactJS interview questions and answers


๐Ÿ“ˆ 34.89 Punkte

๐Ÿ“Œ Hook error handling doesnโ€™t catch errors in ReactJS


๐Ÿ“ˆ 33.26 Punkte

๐Ÿ“Œ How to Test Custom Hook in ReactJS


๐Ÿ“ˆ 33.26 Punkte

๐Ÿ“Œ Use Beat Saber's Level Editor to create your own tracks with your own music


๐Ÿ“ˆ 32.11 Punkte

๐Ÿ“Œ How you can create your own custom chatbot with your own custom data using Google Gemini API all for free


๐Ÿ“ˆ 32.11 Punkte

๐Ÿ“Œ Difference between Action Hook and Filter Hook in WordPress


๐Ÿ“ˆ 31.63 Punkte

๐Ÿ“Œ Using Local Storage in React with Your Own Custom useLocalStorage Hook


๐Ÿ“ˆ 28.17 Punkte

๐Ÿ“Œ Writing Your Own useFetch Hook in React


๐Ÿ“ˆ 28.17 Punkte

๐Ÿ“Œ Huawei releases it's own desktop PC with their own OS based on Linux and their own ARM CPU.


๐Ÿ“ˆ 26.63 Punkte

๐Ÿ“Œ How to Create Skeleton in ReactJs Using PrimeReact/Primefaces UI


๐Ÿ“ˆ 24.83 Punkte

๐Ÿ“Œ Microsoft wants to let you dub videos using your own voice in your own language, new patent reveals


๐Ÿ“ˆ 24.72 Punkte

๐Ÿ“Œ Using Stickers on Your iPhone and How To Create Your Own


๐Ÿ“ˆ 23.23 Punkte

๐Ÿ“Œ The Basics of AI Image Generation: How to create your own AI-generated image using Stable Diffusion on your local machine.


๐Ÿ“ˆ 23.23 Punkte

๐Ÿ“Œ Create your own ChatGPT by scraping your website


๐Ÿ“ˆ 23.23 Punkte

๐Ÿ“Œ Maximizing Your Music Library: How to Download YouTube Music and Create Your Own Collection


๐Ÿ“ˆ 23.23 Punkte

๐Ÿ“Œ A Guide to Debouncing in JavaScript and React | Create a Customย Hook


๐Ÿ“ˆ 23.2 Punkte

๐Ÿ“Œ useRequest: Create a Basic Declarative React hook for Managing API Requests as an Alternative to useQuery


๐Ÿ“ˆ 23.2 Punkte

๐Ÿ“Œ How to Create a Custom React Hook โ€“ a Hands-on Tutorial


๐Ÿ“ˆ 23.2 Punkte

๐Ÿ“Œ React Magic: How to Create a Custom Hook for Seamless Data Retrieval.


๐Ÿ“ˆ 23.2 Punkte

๐Ÿ“Œ How to create useMemo hook in vanilla js?


๐Ÿ“ˆ 23.2 Punkte

๐Ÿ“Œ How to create useCallback hook in vanilla js?


๐Ÿ“ˆ 23.2 Punkte

๐Ÿ“Œ How to easily hook up your XPS 13 to your TV


๐Ÿ“ˆ 22.78 Punkte

๐Ÿ“Œ How to easily hook up your XPS 13 to your TV


๐Ÿ“ˆ 22.78 Punkte

๐Ÿ“Œ ReactJs Chat Component: Easily add chat to your react app.


๐Ÿ“ˆ 20.93 Punkte

๐Ÿ“Œ Using Web Worker API In Your Reactjs Project To Enhance Performance


๐Ÿ“ˆ 20.93 Punkte

๐Ÿ“Œ 12 Essential Skills Your ReactJS Developers Should Have


๐Ÿ“ˆ 20.93 Punkte

๐Ÿ“Œ ReactJS With ChatGPT: Building Conversational AI Into Your Web Apps


๐Ÿ“ˆ 20.93 Punkte

๐Ÿ“Œ Free Icons for your reactjs and web applications


๐Ÿ“ˆ 20.93 Punkte

๐Ÿ“Œ Create Your Own CMD.XLS


๐Ÿ“ˆ 19.75 Punkte

๐Ÿ“Œ Create Your Own CMD.XLS


๐Ÿ“ˆ 19.75 Punkte

๐Ÿ“Œ Howto CreateCertGUI: Create Your Own Certificate On Windows (OpenSSL Library)


๐Ÿ“ˆ 19.75 Punkte











matomo