Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm getting error after upgrading to Material UI 4 - withStyles

I'm getting the following error after upgrading to MUI v4.0.2 from v3.9.x:

You must pass a component to the function returned by connect. Instead received {"propTypes":{},"displayName":"WithStyles(MyComponent)","options":{"defaultTheme":{"breakpoints":{"keys":["xs","sm","md","lg","xl"],"values": ...

MyComponent:

import { withStyles } from '@material-ui/core/styles'

const getStyles = theme => ({
  fooBar: {
    ...
  },
})

...
export default withStyles(getStyles)(MyComponent)

MyContainer:

import { connect } from 'react-redux'
import MyComponent from './MyComponent'
...
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent)

How to migrate withStyles?

like image 977
haxpanel Avatar asked Jun 04 '19 17:06

haxpanel


People also ask

What is material UI withStyles?

Material-UI withStyles was the primary method for wrapping a component and passing style props to it. It is still a valid method to use today if for some reason you are still using higher-order components or still using React lifecycle methods.

How do I import makeStyles into material UI?

instead of makeStyles you can just create object with styles ( style={your styles} ) inside functional component and then use sx property( sx={style} ) on MUI component inside that functional component.

Is material UI completely free?

Pricing - MUI. Ready-to-use foundational components, free forever.

Is material UI good for performance?

Material UI provides developers with material-based UI components. They help save some time on design and app development. But since Material UI is mostly a set of UI components, it doesn't offer such a great boost to the development speed as Bootstrap does.


1 Answers

Version 5.0.7 and earlier of react-redux performed the following validation on the component passed to connect:

https://github.com/reduxjs/react-redux/blob/v5.0.7/src/components/connectAdvanced.js#L91

    invariant(
      typeof WrappedComponent == 'function',
      `You must pass a component to the function returned by ` +
      `${methodName}. Instead received ${JSON.stringify(WrappedComponent)}`
    )

With the introduction of React.forwardRef (which is used heavily in Material-UI v4) and other features introduced in React 16.8 (hooks), it is possible to have a component type that is not a function.

More recent versions of react-redux instead use isValidElementType from the react-is package. This correctly recognizes the component types returned by forwardRef and other methods.

I believe versions 5.1 and later of react-redux should all work fine without erroneously causing the error mentioned in the question.

like image 124
Ryan Cogswell Avatar answered Sep 20 '22 01:09

Ryan Cogswell