Skip to content

Instantly share code, notes, and snippets.

@some1else
Last active December 18, 2016 13:36
Show Gist options
  • Save some1else/e6f39691f7345d35b66bbfa52bec6853 to your computer and use it in GitHub Desktop.
Save some1else/e6f39691f7345d35b66bbfa52bec6853 to your computer and use it in GitHub Desktop.
Using Class.prototype.state for default initial state.
class App extends React.Component {
startWondering = (e) => {
this.setState({ feeling: 'wondering' })
}
render() {
const { feeling } = this.state
const { feeling: initialFeeling } = this.__proto__.state
if (feeling != initialFeeling) {
return <p>I am now { feeling }. Usually I am { initialFeeling }.</p>
} else {
return <p onClick={ this.startWondering }>I think I am always { feeling }.</p>
}
}
}
// Are we missing an idiomatic way to do this inside the class definition?
App.prototype.state = { feeling: "fine with it" }
React.render(<App />, document.getElementById('app'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment