wcjohnson / redux-components

A component model for Redux state trees.
https://wcjohnson.github.io/redux-components
MIT License
4 stars 1 forks source link

ES6 Classes as component classes #7

Closed wcjohnson closed 7 years ago

wcjohnson commented 7 years ago

Now that CoffeeScript 2.0 is available, and we're on the Babel toolchain, we can deliver proper support for ES6 classes by analogy with the react model. Something like:

class MyComponent extends ReduxComponent
  # construction
  constructor: -> super

  # reducer
  getReducer: -> (state={}, action) -> state

  # lifecycle
  componentWillMount: ->
  componentDidMount: ->
  componentWillUnmount: ->

Problems will arise (as they do with React) in things like magic-binding functions. We will also need an API for doing the magic we do with selectors and actionCreators right now.

wcjohnson commented 7 years ago

API Idea 1: Decorators

I think this is the best way to go from a readability standpoint, however, it will only be possible if the new TC39 decorators (with finisher support) make it into the standard. Babel hasn't even implemented these yet so that is probably not a good sign. If they do, though, this would be pretty neat:

class MyComponent extends ReduxComponent
  # selectors
  @selector
  getSomeStuff: (state) -> state.stuff

  @selector({memoized: true})
  getSomeComplicatedStuff: (state) -> longCalculation(state.stuff)

  # action creators
  @action
  update: (newStuff) -> { type: @MODIFY_STUFF, payload: newStuff }

  # (this would create an action dispatcher `this.doUpdateMore`
  @action({withDispatcherWithPrefix: 'do'})
  updateMore: (moreStuff) -> {type: @MODIFY_MORE_STUFF, payload: moreStuff }
wcjohnson commented 7 years ago

The 0.4.x branch implements this functionality completely, including decorators, using the transform-decorators-legacy Babel transform.