Skip to content

Instantly share code, notes, and snippets.

@almarklein
Created December 13, 2017 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almarklein/2c4a37a6689fd55b41aa33f8957877c7 to your computer and use it in GitHub Desktop.
Save almarklein/2c4a37a6689fd55b41aa33f8957877c7 to your computer and use it in GitHub Desktop.
## Option 1 - strings for verbatim html, dicts for elements with attributes
class MyReactlikeWidget(ReactLike):
name = event.StringProp('john', settable=True)
def render(self):
return ['greet: ',
dict(type='b',
onclick=lambda:print('clicked!'),
innerHTML='hi ' + self.name),
'<br><button onclick="alert(\'told you!\')">dont click me</button>',
dict(type='button',
onclick=lambda:window.alert('told you'),
innerHTML='dont click me'),
]
## Option 2 - Element class to ease the spelling, second arg can be dict with args
class MyReactlikeWidget(ReactLike):
name = event.StringProp('john', settable=True)
def render(self):
return [Element('span', 'greet: '),
Element('b', dict(onclick=lambda:print('clicked')),
'hi ',
self.name),
Element('br'),
Element('span', '<button onclick="alert(\'told you!\')">dont click me</button>'),
Element('button', dict(onclick=lambda:window.alert('told you')),
'dont click me'),
]
@almarklein
Copy link
Author

almarklein commented Dec 13, 2017

Resulting in something like:
image

Setting the name will re-render.

@almarklein
Copy link
Author

BTW, I added that button twice in two different ways to illustrate the difference between verbatim html and the more structured syntax. I think it makes sense to support verbatim html at the least. The question is more how to let users write more structured dom structures.

BTW: at the moment the widget will recreate the DOM nodes. But eventually it could apply more of a diff approach for efficiency.

@Korijn
Copy link

Korijn commented Dec 14, 2017

This is awesome! I really like the second example!

Besides more structure (nesting widgets?) it would be cool to also extend the example with an input box that can be used to update self.name. That would complete the unidirectional feedback loop touted by react-likes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment