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

后端 未结 2 1720
無奈伤痛
無奈伤痛 2021-01-18 07:09

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 rec

相关标签:
2条回答
  • 2021-01-18 07:48

    This is how I do it:

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

    Define your styles object: look at the material-ui examples for guidance

    const styles => ({
      root: {
        display: 'flex',
      }
    });
    

    Then export the component using your styles:

    export default withStyles(styles)(YourComponent);
    
    0 讨论(0)
  • 2021-01-18 08:05

    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.

    0 讨论(0)
提交回复
热议问题