The Dead Man
The Dead Man

Reputation: 5566

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

Upvotes: 0

Views: 1184

Answers (1)

kind user
kind user

Reputation: 41893

First of all, the Class name should be capitalized:

class Thankyoupayment extends Component { 

Secondly, you can't use const as a Class method:

resizeWindow = () => {

Upvotes: 1

Related Questions