WebReflection / hyperHTML

A Fast & Light Virtual DOM Alternative
ISC License
3.07k stars 112 forks source link

Parameter in render() of hyperHTML.component #104

Closed kidwm closed 7 years ago

kidwm commented 7 years ago

Pass state like in react, see react-future:

class Clock extends hyper.Component {
  get defaultState() { return {date: new Date()}; }
  render(state) {
    return this.html`
      <div>
        <h1>Hello, world!</h1>
        <h2>It is ${state.date.toLocaleTimeString()}.</h2>
      </div>`;
  }
}

Or any other better use case?

WebReflection commented 7 years ago

Use this.state instead. The render is a public method and components are usable outside hyperHTML too. State belongs to the instance, not to who invokes the render. Moreover, if you invoke the render you can pass whatever you want keeping this.state accessible.

WebReflection commented 7 years ago

P.S. if you really want to have a state parameter, you can always define your render function as such:

class Clock extends hyper.Component {
  get defaultState() { return {date: new Date()}; }
  render(state = this.state) {
    return this.html`
      <div>
        <h1>Hello, world!</h1>
        <h2>It is ${state.date.toLocaleTimeString()}.</h2>
      </div>`;
  }
}
kidwm commented 7 years ago

Got it, thanks for you explanation.