Snippets Collections
const [scroll, setScroll] = useState(false);
 useEffect(() => {
   window.addEventListener("scroll", () => {
     setScroll(window.scrollY > 50);
   });
 }, []);


className={scroll ? "bg-black" : "bg-white"}
const [scroll, setScroll] = useState(false);
 useEffect(() => {
   window.addEventListener("scroll", () => {
     setScroll(window.scrollY > 500);
   });
 }, []);

// Se implementa asi
<div className={scroll ? "box-buscador is--fixed" : "box-buscador"}>
import { useState } from "react"

export default function useToggle(defaultValue) {
  const [value, setValue] = useState(defaultValue)

  function toggleValue(value) {
    setValue(currentValue =>
      typeof value === "boolean" ? value : !currentValue
    )
  }

  return [value, toggleValue]
}
import { useState, useEffect, useRef } from "react";
export default function App() {
  const [day, setDay] = useState("Monday");
  const prevDay = usePrevious(day);
  const getNextDay = () => {
    if (day === "Monday") {
      setDay("Tuesday")
    } else if (day === "Tuesday") {
      setDay("Wednesday")
    } else if (day === "Wednesday") {
      setDay("Thursday")
    } else if (day === "Thursday") {
      setDay("Friday")
    } else if (day === "Friday") {
      setDay("Monday")
    }
  }
  return (
    <div style={{padding: "40px"}}>
      <h1>
        Today is: {day}<br />
        {
          prevDay && (
            <span>Previous work day was: {prevDay}</span>
          )
        }
      </h1>
      <button onClick={getNextDay}>
        Get next day
      </button>
    </div>
  );
}
function usePrevious(val) {
  const ref = useRef()

  useEffect(() => {
    ref.current = val
  }, [val])
  return ref.current
}
// utils/useDeviceDetect.js
import React from "react";

export default function useDeviceDetect() {
  const [isMobile, setMobile] = React.useState(false);

  React.useEffect(() => {
    const userAgent =
      typeof window.navigator === "undefined" ? "" : navigator.userAgent;
    const mobile = Boolean(
      userAgent.match(
        /Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i
      )
    );
    setMobile(mobile);
  }, []);

  return { isMobile };
}
star

Fri May 19 2023 18:04:22 GMT+0000 (Coordinated Universal Time)

#hooks #scroll
star

Sat May 13 2023 05:57:46 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/44205000/how-to-add-class-in-element-on-scroll-react-js

#hooks #custom
star

Tue May 09 2023 12:47:44 GMT+0000 (Coordinated Universal Time) https://codesandbox.io/s/quirky-glade-brmwg6?file

#hooks #custom
star

Sun Feb 26 2023 04:30:38 GMT+0000 (Coordinated Universal Time) https://dev.to/arafat4693/15-useful-react-custom-hooks-that-you-can-use-in-any-project-2ll8

#react #hooks
star

Wed Dec 28 2022 09:32:11 GMT+0000 (Coordinated Universal Time) https://www.coursera.org/learn/advanced-react/supplement/DcqzI/solution-create-your-own-custom-hook-useprevious

#react.js #useref #ref #hooks #customhook
star

Sat Feb 19 2022 13:20:01 GMT+0000 (Coordinated Universal Time) https://thesmartcoder.dev/awesome-react-hooks/

#react #hooks
star

Sat Feb 19 2022 13:17:17 GMT+0000 (Coordinated Universal Time) https://egghead.io/lessons/react-introduction-to-refactoring-a-react-application-to-react-hooks

#react #hooks
star

Wed Oct 21 2020 17:34:45 GMT+0000 (Coordinated Universal Time) https://medium.com/code-artistry/how-to-create-a-custom-usedevicedetect-react-hook-f5a1bfe64599

#react.js #hooks

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension