javascript - Why is this in React event null?
習慣沉默
習慣沉默 2017-05-19 10:31:29
0
1
484

Why is this null?

class App extends Component {


    handleClick(e) {
        console.log(this)//为什么是 null?
    }

    render() {
        return (
         
            <h2 onClick={this.handleClick}>React</h2>

        );
    }
}

export default App;

How to explain the following situation?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button onclick="func()">click</button> // 打印 undefined
<button onclick="app.handleClick()">click</button> // 打印 App
<script>

    class App {
        handleClick() {
            console.log(this)
        }
    }
    const app = new App
    const func = app.handleClick
</script>
</body>
</html>

So when will undefined be printed, when will null be printed, and when will it be printed normally?

習慣沉默
習慣沉默

reply all(1)
PHPzhong
 <h2 onClick={this.handleClick.bind(this)}>React</h2>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!