【发布时间】:2021-05-25 12:29:36
【问题描述】:
我有一个 JSX 文件 MyComponent.jsx 定义如下;
import compose from 'compose-function'
const MyComponent = () => {
//some hooks like useState, useEffect here
// finally returns some JSX
return (
<>
</>
)
}
function MyComponentData(props) {
let StyledMyComponent = compose(
withStyle(style),
withTranslation('translation'),
withMyApi()
)(MyComponent);
return (
<MyContextProvider>
<StyledMyComponent />
</MyContextProvider>
);
}
export default MyComponentData;
现在“withMyApi”如下所示(在单独的 Api.jsx 文件中定义);
export function withMyApi() {
// Extra function wrapper. Is this required to compose multiple HOCs ?
return function(WrappedComponent) {
class MyApiURLs extends React.Component {
constructor(props) {
super(props);
this.api = {
dataSources: dataSources
}
}
render() {
const { title, content } = this.props;
return (
<div>
<WrappedComponent api={this.api} {...this.props} />
</div>
);
}
}
return MyApiURLs;
}
}
现在是我的问题;
- 我正在尝试了解“compose-function”依赖项的用法。是否可以组合样式、进行 API 调用的类、翻译等内容?
- 在 withMyApi 中,我看到了一个额外的函数包装器。是否需要组成多个 HOC?我的意思是整个语法很混乱。虽然我了解 HOC,但我只是想了解它的语法用法,特别是在 MyComponent.jsx 中如何使用它的上下文中。
- 在这种情况下,“WrappedComponent”究竟指的是什么?这就是我撰写的下一个圆括号中的内容,即 MyComponent 在这种情况下?
【问题讨论】:
标签: javascript reactjs react-hooks jsx higher-order-functions