【发布时间】:2020-06-30 23:01:45
【问题描述】:
我自己学习材料 UI 并遇到了这个 HOC 模式方法 withStyle HOC API,我尝试用简单的样式(只是字符串)自己实现,以了解这个 hoc(withStyle) 函数如何使用这个 hoc(名为 HigherOrderComponent) 函数在代码中工作。
这是 App.js 并且在 index.js 中渲染<App/> 这个
import React from 'react';
import customWithStyle from './customWithStyle';
import Button from '@material-ui/core/Button';
function HigherOrderComponent(props) {
return <Button color={props}>Higher-order component</Button>;
}
const someStyle="primary";
export default customWithStyle(someStyle)(HigherOrderComponent);
我尝试编写的代码在 customWithStyle.js 文件中
import React from 'react'
const customWithStyle=(style)=>(Hoc)=>{
//console.log(style);
console.log(<Hoc/>)
return <Hoc props={style}/>
}
export default customWithStyle ;
我遇到了错误
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
有人可以帮忙吗。
【问题讨论】:
标签: javascript reactjs material-ui higher-order-components