javascript - 使用 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);
解决方案
在我看来,目前在 javascript 中使用装饰器仍然是一个坏主意。
它们仍然是第 2 阶段的提案:https ://github.com/tc39/proposals 。
根据这个过程https://tc39.es/process-document/,并不意味着它一定会包含在语言中。
如果你确实使用了装饰器,它可能会使用 typescript 或 babel ( legacy-decorators)。如果语言中的实际功能不匹配或被拒绝,您可能需要稍后更改一些代码来修复。
推荐阅读
- javascript - Parsing a nested object does not traverse the whole list with javascript
- jquery - onClick hide/show after page refreshed
- c++ - Detailed explanation of transfer count members in IO_COUNTERS filled by GetProcessIoCounters
- express - Express + Axios - Pipe request to another server
- linux - Can you tell me what "crus" means in "LDFLAGS = crus $@"?
- python - My tkinter window isn't showing everything it should
- python - Discord.py 添加命令
- angular - Angular components not binding attributes after update from Angular 7 to Angular 10
- jupyter - Remove source code from notebook cell with Nbconvert 6.x
- c# - Change Navigation Bar Color for Single Page in Xamarin