【React】React全家桶(二)

React+Antd+Router+Axios+Redux+Saga+Less

Posted by ARTROY on 2019-09-20

是否添加装饰器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// (1)
@connect(
state=>({num:state.counter}),
dispatch=>({
add: ()=>dispatch({type:"add"}),
minus: ()=>dispatch({type:"add"})
})
)
// (2)
const mapStateToProps = state => ({ num: state.counter });
const mapDispatchToProps = { add, minus };
@connect(
mapStateToProps,
mapDispatchToProps
)
class Com extends React.Component {
...
}
export default Com;

1、安装

1
2
npm install --save-dev @babel/plugin-proposal-decorators
npm install --save-dev babel-plugin-transform-decorators-legacy

2、设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 方法一
"babel": {
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
"transform-decorators-legacy"
],
"presets": [
"react-app"
]
}

// 方法二:config-overrides.js
const { injectBabelPlugin } = require("react-app-rewired");
module.exports = function override(config, env) {
config = injectBabelPlugin(
// 在默认配置基础上注入
// 插件名,插件配置
["import", { libraryName: "antd", libraryDirectory: "es", style: "css" }],
config
);

config = injectBabelPlugin(
["@babel/plugin-proposal-decorators", { legacy: true }],
config
);

return config;
};


支付宝打赏 微信打赏

欣赏此文,打赏一下



-->