ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

React学习笔记——类中的方法内部的this指向

2021-04-08 12:57:40  阅读:176  来源: 互联网

标签:weatherClick 指向 实例 isHot 笔记 React state Weather 类中


类中的方法内部的this指向

在用react实现一个点击切换内容小demo时的,发现类中方法内部的this是指向undefined的。

代码如下:

<div id="test"></div>
    <script type="text/javascript" src="../JS/react.development.js"></script>
    <script type="text/javascript" src="../JS/react-dom.development.js"></script>
    <script type="text/javascript" src="../JS/babel.min.js"></script>

    <script type="text/babel">
        class  Weather extends React.Component{
            constructor(props){
                super(props)
                this.state ={
                    isHot:false
                }
            }
            render(){
                return <h1 onClick={this.weatherClick}>今天天气很{this.state.isHot ? '炎热':'凉爽'}</h1>
            }
            weatherClick(){
                
                console.log(this);
            }
        }
        ReactDOM.render(<Weather/>,document.getElementById('test'))
    </script> 

点击结果如下:
在这里插入图片描述
原因:
①首先,weatherClick放在那里? –Weather的原型对象上,供实例使用;
由于weatherClick在代码中是作为onClick的回调,所以不是通过实例调用的,是直接调用的,因此并不是这个组件实例了;
②此外,由于类中的方法默认开启了局部的严格模式,因此weatherClick中的this为undefined


验证严格模式会导致函数中的this指向变为undefined

代码如下:
在这里插入图片描述

执行结果:
在这里插入图片描述
我们发现,在普通函数中,this的指向是window
但是在严格模式下,this的指向变味了undefined

修改方法:使用bind方法对weatherClick方法进行重包装。

<div id="test"></div>
    <script type="text/javascript" src="../JS/react.development.js"></script>
    <script type="text/javascript" src="../JS/react-dom.development.js"></script>
    <script type="text/javascript" src="../JS/babel.min.js"></script>

    <script type="text/babel">
        class  Weather extends React.Component{
            constructor(props){
                super(props)
                this.state ={
                    isHot:false
                }
                // 解决weatherClick方法中的this指向问题
                this.weatherClick = this.weatherClick.bind(this);
            }
            render(){
                return <h1 onClick={this.weatherClick}>今天天气很{this.state.isHot ? '炎热':'凉爽'}</h1>
            }
            weatherClick(){
                // weatherClick放在那里? --Weather的原型对象上,供实例使用
                // 由于weatherClick是作为onClick的回调,所以不是通过实例调用的,是直接调用的
                // 类中的方法默认开启了局部的严格模式,因此weatherClick中的this为undefined
                console.log(this);
            }
        }
        ReactDOM.render(<Weather/>,document.getElementById('test'))
    </script> 

核心:使weatherClick的this重新绑定为这个Weather组件实例

// 解决weatherClick方法中的this指向问题
this.weatherClick = this.weatherClick.bind(this);

执行效果:
在这里插入图片描述
完整demo代码:

<div id="test"></div>
    <script type="text/javascript" src="../JS/react.development.js"></script>
    <script type="text/javascript" src="../JS/react-dom.development.js"></script>
    <script type="text/javascript" src="../JS/babel.min.js"></script> 

    <script type="text/babel">
        class  Weather extends React.Component{
            constructor(props){
                super(props)
                this.state ={
                    isHot:false
                }
                // 解决weatherClick方法中的this指向问题
                this.weatherClick = this.weatherClick.bind(this);
            }
            render(){
                return <h1 onClick={this.weatherClick}>今天天气很{this.state.isHot ? '炎热':'凉爽'}</h1>
            }
            weatherClick(){
                // weatherClick放在那里? --Weather的原型对象上,供实例使用
                // 由于weatherClick是作为onClick的回调,所以不是通过实例调用的,是直接调用的
                // 类中的方法默认开启了局部的严格模式,因此weatherClick中的this为undefined
                console.log(this)
                const isHot = this.state.isHot;
                this.setState({
                    isHot:!isHot
                })
            }
        }
        ReactDOM.render(<Weather/>,document.getElementById('test'))
    </script> 

标签:weatherClick,指向,实例,isHot,笔记,React,state,Weather,类中
来源: https://blog.csdn.net/weixin_45664402/article/details/115516158

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有