Skip to content

Commit

Permalink
[@mantine/hooks] use-mounted: Init hook
Browse files Browse the repository at this point in the history
  • Loading branch information
rtivital committed Mar 12, 2024
1 parent 7f4c988 commit be8892d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/mdx/data/mdx-hooks-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@ export const MDX_HOOKS_DATA: Record<string, Frontmatter> = {
'useMutationObserver',
'Subscribe to changes being made to the DOM tree'
),
useMounted: hDocs('useMounted', 'Returns true if the component is mounted'),
};
1 change: 1 addition & 0 deletions docs/src/mdx/mdx-pages-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export const MDX_PAGES_GROUPS: MdxPagesGroup[] = [
MDX_DATA.useIsomorphicEffect,
MDX_DATA.useLogger,
MDX_DATA.useShallowEffect,
MDX_DATA.useMounted,
],
},
],
Expand Down
1 change: 1 addition & 0 deletions docs/src/pages/changelog/7-7-0.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ New [useMutationObserver](/hooks/use-mutation-observer) hook:
## Other changes

- [SegmentedControl](/core/segmented-control) indicator positioning logic has been migrated to [FloatingIndicator](/core/floating-indicator). It is now more performant and works better when used inside elements with `transform: scale()`.
- New [use-mounted](/hooks/use-mounted) hook
27 changes: 27 additions & 0 deletions docs/src/pages/hooks/use-mounted.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Layout } from '@/layout';
import { MDX_DATA } from '@/mdx';

export default Layout(MDX_DATA.useMounted);

## Usage

`useMounted` hook returns `true` if component is mounted and `false` if it's not.

```tsx
import { useMounted } from '@mantine/hooks';

function Demo() {
const mounted = useMounted();
return (
<div>
{mounted ? 'Component is mounted' : 'Component is not mounted'}
</div>
);
}
```

## Definition

```tsx
function useMounted(): boolean;
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const SegmentedControlStylesApi: StylesApiData<SegmentedControlFactory> =
input: 'Input element (`input[type="radio"]`), hidden by default',
label: 'Label element associated with input',
indicator: 'Floating indicator that moves between items',
innerLabel: 'Wrapper of label element children',
},

vars: {
Expand Down
1 change: 1 addition & 0 deletions packages/@mantine/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export { useHeadroom } from './use-headroom/use-headroom';
export { useEyeDropper } from './use-eye-dropper/use-eye-dropper';
export { useInViewport } from './use-in-viewport/use-in-viewport';
export { useMutationObserver } from './use-mutation-observer/use-mutation-observer';
export { useMounted } from './use-mounted/use-mounted';

export type { UseMovePosition } from './use-move/use-move';
export type { OS } from './use-os/use-os';
Expand Down
7 changes: 7 additions & 0 deletions packages/@mantine/hooks/src/use-mounted/use-mounted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useEffect, useState } from 'react';

export function useMounted() {
const [mounted, setMounted] = useState(false);
useEffect(() => setMounted(true), []);
return mounted;
}

0 comments on commit be8892d

Please sign in to comment.