Skip to main content

hooks

@morfeo/hooks expose a set of hooks to easily use morfeo inside a react context.

React v18

To properly use Morfeo in a React environment, you need to have a version of react >= 18

Installation

# npm
npm i @morfeo/hooks

# yarn
yarn add @morfeo/hooks

Advanced

useTheme

Use this hook to get the current theme and use it inside a components:

Live Editor
Result
Loading...
note

If you already know the core API useTheme is the equivalent of morfeo.getTheme(), the main difference is that useTheme subscribes your component to theme changes and force a re-render when they'll happen, for example when dark mode is triggered.

Most of the time you don't need the whole theme, but just a slice or single value, in this cases useThemeSlice and useThemeValue will give you only the part of the theme you want:

useThemeSlice

Live Editor
Result
Loading...

useThemeValue

Live Editor
Result
Loading...

useCurrentTheme

A hook to retrieve the current theme name and a callback to change it, they correspond to morfeo.getCurrentTheme and morfeo.setCurrentTheme of the core-api.

Live Editor
Result
Loading...

useStyles

If you don't need the theme, but to generate a style based on the theme; The hooks useStyles and useStyle are made for this reason:

Live Editor
Result
Loading...

useStyle

Use it if you need to generate just one style:

Live Editor
Result
Loading...
note

Just like useTheme, useStyles and useStyle are the equivalent of the core API's morfeo.resolve(style) but they force a re-render when the theme changes.

Advanced

useSyncMorfeo

This hook is used under the hood in all of the others hooks, you'll probably never need to use it directly.

By using useSyncMorfeo the component is subscribed to theme changes, which means that if the theme changes the component will re-render:

Live Editor
Result
Loading...

If instead we use directly the morfeo instance from the core-api without using useSyncMorfeo, the component will not re-render when the theme changes:

Live Editor
Result
Loading...

useProps

Use it to get the default props of a components, a common use is to merge the defaut props with the current props:

Live Editor
Result
Loading...