I want to know why I put _isMounted=false; at the beginning of myClass appears the following error: "SyntaxError: Unexpected token". I'm following the second answer in this thread: Can't perform a React state update on an unmounted component.
class myClass extends Component {
_isMounted = false;
constructor(props) {
super(props);
this.state = {
test: ""
};
}
}
Thanks for your help.
This is my code, I was trying the code above to fix the following error " "Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method" from the code below:
import React, {Component} from 'react'
const months = ["January", "February", "March", "April", "May", "June", "July", "August"," September", "November", "December"]
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
const usaTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
const d = new Date(usaTime)
export default class DateTime extends React.Component {
constructor() {
super()
this.state = {
day: d.getDay(),
month: d.getMonth(),
date: d.getDate(),
year: d.getFullYear(),
time: d.toLocaleTimeString()
}
this.countingSecond = this.countingSecond.bind(this)
}
countingSecond() {
this.setState({
day: d.getDay(),
month: d.getMonth(),
date: d.getDate(),
year: d.getFullYear(),
time: d.toLocaleTimeString()
})
}
componentDidMount() {
setInterval(this.countingSecond, 1000)
}
componentWillUnmount(){
// setInterval(this.countingSecond, 1000)
}
render() {
return(
<div className="timeclock-main">
<h5>{days[this.state.day] + ', ' + months[this.state.month] + ' ' + this.state.date + ', ' + this.state.year }</h5>
<h3>{this.state.time}</h3>
</div>
)
}
}