从v3.9.x升级到MUI v4.0.2后,出现以下错误:
您必须将组件传递给connect返回的函数。而是收到{“ propTypes”:{},“ displayName”:“ WithStyles(MyComponent)”,“ options”:{“ defaultTheme”:{“ breakpoints”:{“ keys”:[“ xs”,“ sm”,“ md“,” lg“,” xl“],”值“:…
MyComponent:
import { withStyles } from '@material-ui/core/styles' const getStyles = theme => ({ fooBar: { ... }, }) ... export default withStyles(getStyles)(MyComponent)
我的容器:
import { connect } from 'react-redux' import MyComponent from './MyComponent' ... export default connect(mapStateToProps, mapDispatchToProps)(MyComponent)
如何迁移withStyles?
withStyles
react-redux 5.0.7及更低版本对传递给的组件执行了以下验证connect:
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)}` )
通过引入React.forwardRef(在Material-UI v4中大量使用)和在React 16.8中引入的其他功能(挂钩),有可能具有 不是 函数的组件类型。
React.forwardRef
最近的版本做出反应,终极版改用isValidElementType从react- is包。这样可以正确识别由forwardRef和其他方法返回的组件类型。
react- is
forwardRef
我相信react-redux的5.1版和更高版本应该都能正常工作,而不会错误地引起问题中提到的错误。