【发布时间】:2019-08-20 11:15:06
【问题描述】:
我正在尝试将实例方法添加到包装的组件中。我通过扩展包装组件的原型来做到这一点:
const useAppContext = () => WrappedComponent => {
WrappedComponent.prototype.$context = { abc: 'abc' };
return class extends React.Component {
render() {
return <WrappedComponent {...this.props} />;
}
};
};
@useAppContext()
class NavigationLinkList extends Component {
render() {
console.log('this.$context', this.$context);
};
};
虽然这个有效我不确定是否有更清洁的方法来做到这一点,一种是“常规”的反应方式。我在文档中找不到任何东西,这种方式似乎有点像 hack。还有其他(更清洁的?)方法吗?
【问题讨论】:
标签: javascript reactjs higher-order-components