0

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

1 Answer 1

1

First of all, the Class name should be capitalized:

class Thankyoupayment extends Component { 

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

resizeWindow = () => {
Sign up to request clarification or add additional context in comments.

4 Comments

now I am getting the following error react\resources\js\pages\Thankyou\thankyoupayment.js: Support for the experimental syntax 'classProperties' isn't currently enabled (8:17): what do I needs to change?
@user9964622 Oh, seems like you don't have babel that will compile yet experimental class fields. Try to add babel or a specified babel extension. You can also change that arrow function into a es5 function (handleResize() { ... }) but then you will probably have to bind it inside constructor.
now no errors but when I try to resize the window there is no console.log('resize me');
@user9964622 Just bind it inside constructor constructor(props){ super(props); this.handleResize = this.handleResize.bind(this); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.