简体   繁体   English

如何在反应组件中创建窗口事件侦听器?

[英]How to create window event listener in react component?

I have a simple react component for my app, I would like to add resize function using window add event listener我的应用程序有一个简单的反应组件,我想使用窗口添加事件侦听器添加调整大小功能

Here is my solution这是我的解决方案

import React, { Component } from 'react';

class thankyoupayment extends Component {

    const resizeWindow = () =>{
        console.log('Resize me');
    }

    componentDidMount() {
        window.addEventListener('resize', this.resizeWindow);
     }

    render() {
        return (
            <VideoContainer>
                    <video></video>
            </VideoContainer>
        );
    }
}

const VideoContainer =styled.div`
        display: flex;
        justify-content: center;
`

Unfortunately when I run I am gettin the following error不幸的是,当我运行时,出现以下错误

Unexpected token (8:10)

What do I need to change to solve the problem?我需要更改什么才能解决问题? React Newbie as hell像地狱一样反应新手

First of all, the Class name should be capitalized:首先,类名应该大写:

class Thankyoupayment extends Component { 

Secondly, you can't use const as a Class method:其次,您不能将const用作 Class 方法:

resizeWindow = () => {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM