Skip to content

Instantly share code, notes, and snippets.

@chrisabrams
Created January 15, 2021 22:07
Show Gist options
  • Save chrisabrams/9e140d212098033365a76666a4926af1 to your computer and use it in GitHub Desktop.
Save chrisabrams/9e140d212098033365a76666a4926af1 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
class ManualStateComponent extends Component {
state = {
now: null
}
constructor(props) {
super(props)
}
// Since you don't want to call `setState`, here is a method with the name you prefer
rerender() {
this.setState({
now: Date.now()
})
}
}
// Example Component
class SimpleTimer extends ManualStateComponent {
constructor(props) {
super(props)
this.seconds = 0 // Just a plain variable
}
componentDidMount() {
setTimeout(() => {
this.seconds++;
this.rerender() // rerender!
}, 1000)
}
render() {
return <div>{this.seconds} secs have elapsed...</div>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment