Created
January 15, 2021 22:07
-
-
Save chrisabrams/9e140d212098033365a76666a4926af1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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