首页 > 解决方案 > 使用 React 装饰器是一个好习惯吗?

问题描述

我只是好奇现在哪种做法更好,更有用,在 React 中编写装饰器是否更清洁?

下面的差异示例:

@reduxForm({form: 'exampleForm', onSubmit: formOnSubmit('example-from-action/')})
export default class ExampleForm extends React.Component {
  render() {
    return <div></div>
  }
}
    class ExampleForm extends React.Component {
        render() {
            return <div></div>
          }
    }

    export default reduxForm({form: 'exampleForm', onSubmit: formOnSubmit('example-from-action/')})(ExampleForm);

标签: javascriptreactjsjavascript-decorators

解决方案


在我看来,目前在 javascript 中使用装饰器仍然是一个坏主意。

它们仍然是第 2 阶段的提案:https ://github.com/tc39/proposals 。

根据这个过程https://tc39.es/process-document/,并不意味着它一定会包含在语言中。

如果你确实使用了装饰器,它可能会使用 typescript 或 babel ( legacy-decorators)。如果语言中的实际功能不匹配或被拒绝,您可能需要稍后更改一些代码来修复。


推荐阅读