Pretend it's all in the render.
"changes the state of the component in the render (or somewhere else shouldn't)"
this.state.foo = whatever;
"reads or writes to the DOM when it should not"
if (this.isMounted()) { this.getDOMNode().textContent = whatever }; document.querySelector('title').textContent = whatever;
"does not return the same result every time it is called / displayed
return <div>{Math.random()}</div>
"(possibly the same as the previous one): does not display the same DOM for the same details and state"
if (new Date().getHours()>=9 && new Date().getHours()<=12+5) { return <div>{this.props.fortune}</div> } else { return <div>I'm off the clock</div> }
You mentioned the last example specifically in the commentary, so the right way to handle the time is to get the time in getInitialState and then set a timeout in componentDidMount when changing the time will affect rendering and setState. Thus, your interface will be synchronized with the world.
The naive solution is to just set Interval for a few seconds, which is fine until you have time to optimize it. The real solution here would be something like
var now = Date.now(), closing = new Date(); closing.setHours(12+5, 0, 0, 0), opening = new Date(); opening.setHours(9, 0, 0, 0), delay;
And, like any change in state over time, mixins are often a good way to abstract and reuse.
source share