hurrymaplelad / teact

Build React element trees by composing functions
MIT License
26 stars 6 forks source link

may need a companion to pureComponent to push a given child to stack #10

Open leiyangyou opened 7 years ago

leiyangyou commented 7 years ago

ran into a usecase where I need to wrap a component before pushing the component onto the stack

leiyangyou commented 7 years ago

Let me reopen this, and elaborate a bit on the usecase.

The scenario I have is this, when using react-dnd, I need to sometimes wrap a child inside a decorator.

For example connectDragSource, this is what I have to do with just crel and pureComponent

 T.crel ->
    connectDragSource(do T.pureComponent ->
       T.div
       T.div
    )

Not too bad, what i ended up doing is however this

T.use((teact)->
  T.push = (contents)->
    teact.stack?.push contents
    return contents

  T.build = (contents)->
    previous = teact.resetStack null
    children = contents.apply teact, arguments
    teact.resetStack previous
    children
)

T.push connectDragSource(T.build ->
  T.div
  T.div
)

and I do believe that these two primitives are worthwhile considering to be included in the lib itself.

hurrymaplelad commented 7 years ago

Thanks for writing this up @leiyangyou! I can see how those primitives would be useful. I'd wait for a little more demand before expanding the interface. You could publish your code as a separate mixin (similar to https://github.com/goodeggs/teacup-camel-to-kebab) and we'll revisit based on traction?