1. 事件监听
handleChange(e){ this.setState({ name:e.target.value }) }
<input type="text" value={this.state.name} onChange={(e)=>this.handleChange(e)} />
2. PureComponent Vs Component https://www.jianshu.com/p/c41bbbc20e65
3. React.memo
const withKaikeba = (Component) => { const NewComponent = (props) => { return <Component {...props} name="开课吧高阶组件" />; };return NewComponent; };
import React, {Component} from ‘react‘
import {Button} from ‘antd‘
const withKaikeba = (Component) => {
const NewComponent = (props) => {
return <Component { ...props } name = "开课吧高阶组件" / > ;
};
return NewComponent;
};
const withLog = Component => {
class NewComponent extends React.Component {
render() {
return <Component { ...this.props } />;
}
componentDidMount() {
console.log(‘didMount‘, this.props)
}
} return NewComponent
}
class App extends Component {
render() {
return (
<div className="App">
<h2>hi,{this.props.name}</h2 >
< Button type="primary" > Button < /Button>
</div >
)
}
}
export default withKaikeba(withLog(App))
npm install --save-dev babel-plugin-transform-decorators-legacy
const { injectBabelPlugin } = require("react-app-rewired");
module.exports = function override(config) {
config = injectBabelPlugin(
[
"import",
{
libraryName: "antd",
libraryDirectory: "es",
style: "css"
}
],
config
);
config = injectBabelPlugin(
[
"@babel/plugin-proposal-decorators",
{
legacy: true
}
],
config
);
return config;
};
使用装饰器
import React, { Component } from ‘react‘
import { Button } from ‘antd‘
const withKaikeba = (Component) => {
const NewComponent = (props) => {
return <Component { ...props } name="开课吧高阶组件" />;
};
return NewComponent;
};
const withLog = Component => {
class NewComponent extends React.Component {
render() {
return <Component { ...this.props } />;
}
componentDidMount() {
console.log(Component.name, ‘didMount‘, this.props)
}
} return NewComponent
}
@withKaikeba
@withLog
class App extends Component {
render() {
return (
<div className="App">
<h2>hi,{this.props.name}</h2 >
< Button type="primary" > Button < /Button>
</div >
)
}
}
export default App
7.
原文:https://www.cnblogs.com/it-Ren/p/12219913.html