ES6 variables in classes [SOLVED]

I want to learn how to work with React using ES6 classes, but I got an issue… I searched on stackOverflow, but didn’t get an answer to my issue.

I have the following:

class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {title: 'Quiz app '};
        
        this.i = 0;
    }
    
    componentDidMount() {
        console.log("componentDidMount");
        this.tick();
    }
    
    tick() {
        console.log(this.i); // Getting the Main class for this
        setInterval(function(){
            console.log(this.i); // Getting the window object for this
            this.i++;
        }, 1000);
        
    }
    
    render() {
        return (
            <div>
                <h1>{this.state.title}</h1>
            </div>
        );
    }
};

How can I get the Main reference of i in the setInterval?
I want to change it’s value once a second, incrementing it.

Poster resolved the issue

1 Like