paf31 / purescript-thermite

A simple PureScript wrapper for React
MIT License
350 stars 55 forks source link

API for dispatching actions into elements? #51

Closed teh closed 8 years ago

teh commented 8 years ago

Hi,

I'm currently combining purescript-routing and thermite with a bit of code like the following to dispatch events into the root component:

componentDidMount :: forall props state eff. (React.ReactThis props state -> InputAction -> T.EventHandler) -> R.ComponentDidMount props state (console :: CONSOLE | eff)
componentDidMount dispatch this = do
    matches routing callback
  where
    callback :: Maybe Routes -> Routes -> T.EventHandler
    callback _ RouteA = do
      dispatch this ActionA
    callback _ RouteB = do
      dispatch this ActionB

This works but isn't particularly beautiful. I was wondering if you have an idea / interest for how to add a halogen driver (https://github.com/slamdata/purescript-halogen/blob/v0.5.14/docs/Halogen/Driver.md) like function?

paf31 commented 8 years ago

Did you see that createReactSpec already returns a driver function?

https://github.com/paf31/purescript-thermite/blob/master/docs/Thermite.md#createreactspec

teh commented 8 years ago

Thanks. Yea, that's what I'm using, here's some (ugly) code where I extract the dispatcher:

  let validatedInputSpec = T.createReactSpec validatedInput initialState
  let component = R.createClass ((_.spec validatedInputSpec) { componentDidMount = (componentDidMount (_.dispatcher validatedInputSpec))})
  let reactElement = (R.createFactory component {})

AFAICT the dispatcher needs a ReactThis which I can only get out from within react (e.g. after component mounting).

So this isn't a case of "not possible" but I was wondering whether we could make the API nicer.

After more reflection I guess it's going to be hard because we need the specific component for which to dispatch actions. I could add a helper Thermite.createElement that returns a dispatcher bound to ReactThis but that seems overkill.

teh commented 8 years ago

Created a helper that returns the correctly bound dispatcher but I think it's a bit too specific to be included in Thermite. Closing.