Skip to content

Conversation

nhunzaker
Copy link
Contributor

Sometimes a presenter just wants to subscribe to all state changes. We can't just return this.repo.state, because the view model is only recalculated when a presenter receives new props.

So here's my solution: support returning a function that is given the current repo state:

class MyPresenter extends Presenter {
  viewModel() {
    return state => state
  }
  render() {
    return <p>Test</p>
  }
}

Or, to quickly extract several keys:

class MyPresenter extends Presenter {
  viewModel() {
    return ({ dogs, cats }) => ({ dogs, cats })
  }
  render() {
    return <p>Test</p>
  }
}

We'll also now warn if this.repo.state is returned from the view model. What do you think?

@nhunzaker
Copy link
Contributor Author

nhunzaker commented Sep 13, 2016

I have mixed thoughts on returning a function. On one hand, it's very ergonomic. On the other, it totally invites unexpected rendering inefficiencies. Still I also can't help but wonder if there are other neat things you could do here, like make a view model module.

@cwmanning
Copy link
Member

👍

@cwmanning
Copy link
Member

@nhunzaker easy to add those alternate code examples from the PR description into the docs?

@nhunzaker
Copy link
Contributor Author

Yess. I sure can.

@nhunzaker
Copy link
Contributor Author

@cwmanning done.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 072734f on nh-warn-on-app-state into 0286dd0 on master.

@nhunzaker nhunzaker merged commit 072734f into master Sep 14, 2016
@nhunzaker nhunzaker deleted the nh-warn-on-app-state branch October 12, 2016 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants