Javascript 将redux connect HOC作为类装饰器@connect进行响应
我使用react-redux的connect-HOC形式包装了一个组件:Javascript 将redux connect HOC作为类装饰器@connect进行响应,javascript,reactjs,redux,ecmascript-next,Javascript,Reactjs,Redux,Ecmascript Next,我使用react-redux的connect-HOC形式包装了一个组件: import React, { Component } from 'react'; import { connect } from 'react-redux'; import RegisterForm from './register-form'; import { registerUser } from '../store/api/Auth/actions'; @connect(null, { registerUser
import React, { Component } from 'react';
import { connect } from 'react-redux';
import RegisterForm from './register-form';
import { registerUser } from '../store/api/Auth/actions';
@connect(null, { registerUser: registerUser })
export default class RegisterPage extends Component {
handleSubmit = values =>
this.props.registerUser(values);
render() {
return (
<div>
<h1>Register Here</h1>
<RegisterForm onSubmit={this.handleSubmit} />
</div>
);
}
}
我正在使用带有babel loader的Webpack 4。我的.lrc文件是:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties",
["@babel/plugin-proposal-decorators", { "decoratorsBeforeExport": true }]
]
}
在我看来,connect函数的目标对象是类,但是connect函数正在接收该类的属性描述符。我知道React和Redux团队不鼓励装饰师。这是一个相关的问题:
如何让@connect decorator工作?确保在插件类属性之前插入decorator插件
{
"plugins": [
"@babel/plugin-proposal-decorators",
"@babel/plugin-proposal-class-properties"
]
}
正如您所指出的,我们确实不鼓励一般使用装饰器,特别是使用
connect
作为装饰器。decorator语法和插件仍然不稳定,并且仍在继续更改。@markerikson感谢您的回复。看到其他人在使用它,我很好奇我错过了什么让它工作。这是一个不断变化的景观。如果你执意使用一些没有官方支持的东西,那么你需要多做一点。您需要找到一个版本的babel插件转换装饰器(或-legacy)来实现这一点。您可能还需要找到其他babel插件的匹配版本,这些插件可以在该生态系统中工作。追求这个想法对科学有好处,所以祝你好运请添加一些关于链接资源的信息/描述/上下文。我仍然将描述符作为装饰器函数的第一个也是唯一的参数。
{
"plugins": [
"@babel/plugin-proposal-decorators",
"@babel/plugin-proposal-class-properties"
]
}