MUI Material UI React Hook useTheme() is legacy, what is the alternative?

Viewed 267

If the React Hook useTheme() is legacy, how can one access theme props outside of JSX and CSS and not by using sx prop?

For example, I have a more complex logic in a component and need to access my custom theme properties in some handlers, but I don’t want to use legacy APIs.

1 Answers

Assuming you are using @mui/material...

I believe it is only @mui/styles useTheme that is deprecated (per this page: https://mui.com/system/styles/basics/)

And so you should be able to use the useTheme hook from @mui/material/styles as shown here: https://mui.com/material-ui/customization/theming/#accessing-the-theme-in-a-component

Here's another option I found: https://mui.com/system/styled/#parameter-when-using-function-are-different-for-each-field (allows for the theme to be used directly in the sx prop)

Related