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

Utility hooks additions #867

Merged
merged 8 commits into from
Feb 10, 2025
12 changes: 12 additions & 0 deletions src/core/hooks/useLayoutEffect/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useLayoutEffect as ReactUseLayoutEffect } from 'react';

// When we use useLayoutEffect in a server component, it will throw an error.
// One of the hooks that do not work on server components
// wrapping it this way and using it will make sure errors are not thrown and fail silently
// If it's server, we just return a noop function (no operation)

// Radix UI does it this way - https://github.com/radix-ui/primitives/blob/main/packages/react/use-layout-effect/src/use-layout-effect.tsx
//
const useLayoutEffect = globalThis.document ? ReactUseLayoutEffect : () => {};

export default useLayoutEffect;
18 changes: 18 additions & 0 deletions src/core/hooks/useLayoutEffect/useLayoutEffect.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import useLayoutEffect from './index';
import { render } from '@testing-library/react';

const TestComponent = () => {
// invoke useLayoutEffect and check if the component still mounts as expected
useLayoutEffect(() => {
// mounts
}, []);
return <div>Hello</div>;
};

describe('useLayoutEffect', () => {
test('Test for SSR environment and check if the component still mounts as expected', (done) => {
render(<TestComponent />);
done();
});
kotAPI marked this conversation as resolved.
Show resolved Hide resolved
});
Loading