当前位置:
首页 >
2019第10周知识总结
发布时间:2023/12/6
58
豆豆
生活随笔
收集整理的这篇文章主要介绍了
2019第10周知识总结
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
react 事件绑定 函数写法 文档总结
https://react.docschina.org/docs/handling-events.html
1 通过 constroucor绑定
class Toggle extends React.Component {constructor(props) {super(props);this.state = {isToggleOn: true};// This binding is necessary to make `this` work in the callbackthis.handleClick = this.handleClick.bind(this);}handleClick() {this.setState(prevState => ({isToggleOn: !prevState.isToggleOn}));}render() {return (<button onClick={this.handleClick}>{this.state.isToggleOn ? 'ON' : 'OFF'}</button>);} }2 使用新属性 属性初始化器
class LoggingButton extends React.Component {// This syntax ensures `this` is bound within handleClick.// Warning: this is *experimental* syntax.handleClick = () => {console.log('this is:', this);}render() {return (<button onClick={this.handleClick}>Click me</button>);} }3 回调函数中使用 箭头函数: 这样调用方式有缺点,就是每次 LoggingButton 渲染的时候都会创建一个不同的回调函数。在大多数情况下,这没有问题。然而如果这个回调函数作为一个属性值传入低阶组件,这些组件可能会进行额外的重新渲染。我们通常建议在构造函数中绑定或使用属性初始化器语法来避免这类性能问题。
class LoggingButton extends React.Component {handleClick() {console.log('this is:', this);}render() {// This syntax ensures `this` is bound within handleClickreturn (<button onClick={(e) => this.handleClick(e)}>Click me</button>);} }转载于:https://www.cnblogs.com/WhiteHorseIsNotHorse/p/10518440.html
总结
以上是生活随笔为你收集整理的2019第10周知识总结的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: python应用POP3、IMAP、SM
- 下一篇: Rsa2加密报错java.securit