Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useIsomorphicLayoutEffect React hook #69

Closed
mkubdev opened this issue May 9, 2023 · 2 comments
Closed

useIsomorphicLayoutEffect React hook #69

mkubdev opened this issue May 9, 2023 · 2 comments
Assignees
Labels
javascript Tag for Javasript story metaphore Signs for issues that contain metaphors from punk members published This label is used to indicate issues and pull requests that have been published on the Metaphor Web

Comments

@mkubdev
Copy link
Member

mkubdev commented May 9, 2023

The Back Story about your Javascript Metaphor

Check this gnarly snippet, dudes and dudettes! We've got ourselves a rad custom hook called useIsomorphicLayoutEffect that's ready to shred. This bad boy is like an isomorphic version of useLayoutEffect and can rock out in both server-side rendering and client-side rendering jams. 🎸

The javascript Story!

The hook

export const useIsomorphicLayoutEffect =
  typeof window !== 'undefined' ? useLayoutEffect : useEffect;

Usage

This snipp defines athe hook useIsomorphicLayoutEffect. The purpose of this hook is to provide an isomorphic version of the useLayoutEffect hook, which can be used in both server-side rendering and client-side rendering environments.

To understand why this is useful, let's first take a look at the difference between useLayoutEffect and useEffect.

useEffect is a hook provided by React that allows you to perform side-effects in a functional component after the component has rendered. This can include fetching data from an API, updating the DOM, or subscribing to events.

useLayoutEffect is a similar hook to useEffect, but it runs synchronously immediately after React has performed all DOM updates. This means that any updates to the DOM made by useLayoutEffect will be visible to the user before the browser has a chance to paint the screen. This can result in a smoother and more responsive user experience, but it can also cause performance issues if the updates are too heavy.

Now, let's say you are building a React app that needs to support both server-side rendering and client-side rendering. In a server-side rendering environment, there is no browser DOM available, so running useLayoutEffect would cause an error. However, you still need to perform some kind of side-effect to ensure that the component renders correctly on the server.

This is where useIsomorphicLayoutEffect comes in. By using typeof window !== 'undefined' to check if the code is running in a browser environment, useIsomorphicLayoutEffect can switch between useLayoutEffect and useEffect depending on the environment. This means that your code can be written once and used in both server-side and client-side rendering without any issues.

Example

For example, let's say you have a component that needs to fetch data from an API and render it to the DOM. You can use useIsomorphicLayoutEffect to perform the fetch on the client side using useLayoutEffect for a smoother user experience, while using useEffect on the server side to ensure that the component renders correctly:

// /components/MyComponent.jsx

function MyComponent() {
  const [data, setData] = useState(null);

  useIsomorphicLayoutEffect(() => {
    fetchData().then((response) => setData(response));
  }, []);

  if (!data) {
    return <div>Loading...</div>;
  }

  return <div>{data}</div>;
}
  • Explanation:
    fetchData is a function that returns a Promise that resolves to the data we want to render. useIsomorphicLayoutEffect is used to perform the fetch on the client side using useLayoutEffect, while using useEffect on the server side. The if (!data) check ensures that the component displays a "Loading..." message until the data has been fetched and rendered to the DOM.

This snippet can be usefull for library like GSAP 😁

A Javascript demo/repos link

No response

PayPal Link for Donation (Javascript Storyteller)

No response

@mkubdev mkubdev added metaphore Signs for issues that contain metaphors from punk members javascript Tag for Javasript story labels May 9, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 9, 2023

Hello Punk! It's great having you contribute to this project

Welcome to the community :neckbeard:

If you would like to continue contributing to open source and would like to do it with an awesome inclusive community, you should join our GitHub Organisation - we help and encourage each other to contribute to open source little and often :neckbeard:. Any questions let us know.

@darkterminal
Copy link
Member

Write this story while learning something new. That's an awesome catch!!!

@darkterminal darkterminal reopened this May 9, 2023
@darkterminal darkterminal reopened this May 9, 2023
@github-actions github-actions bot added the published This label is used to indicate issues and pull requests that have been published on the Metaphor Web label May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
javascript Tag for Javasript story metaphore Signs for issues that contain metaphors from punk members published This label is used to indicate issues and pull requests that have been published on the Metaphor Web
Projects
None yet
Development

No branches or pull requests

2 participants