Javascript 高阶组件:函数关键字vs箭头函数
在演示如何创建高阶组件时,大多数示例都使用function关键字 React文档中的以下示例:Javascript 高阶组件:函数关键字vs箭头函数,javascript,reactjs,ecmascript-6,higher-order-components,Javascript,Reactjs,Ecmascript 6,Higher Order Components,在演示如何创建高阶组件时,大多数示例都使用function关键字 React文档中的以下示例: function logProps(WrappedComponent) { return class extends React.Component { componentWillReceiveProps(nextProps) { console.log('Current props: ', this.props); console.log('Next props:
function logProps(WrappedComponent) {
return class extends React.Component {
componentWillReceiveProps(nextProps) {
console.log('Current props: ', this.props);
console.log('Next props: ', nextProps);
}
render() {
// Wraps the input component in a container, without mutating it. Good!
return <WrappedComponent {...this.props} />;
}
}
}
问题是:在function关键字中使用语法有什么区别吗?有什么好处吗?在您的情况下没有区别-它们的行为相同 但在有些情况下情况并非如此——在这个话题上有一个很好的答案
export const collapse = (options) =>
(Trigger) =>
class C extends React.Component {
}