Skip to content

Instantly share code, notes, and snippets.

@valerysntx
Created August 22, 2017 21:35
Show Gist options
  • Save valerysntx/fec92ceb0e336e51a5b0e70cdc9a6b43 to your computer and use it in GitHub Desktop.
Save valerysntx/fec92ceb0e336e51a5b0e70cdc9a6b43 to your computer and use it in GitHub Desktop.
pythonic react
# Helper functions
def h(elm_type, props='', *args):
return React.createElement(elm_type, props, *args)
def render(react_element, destination_id, callback=lambda: None):
container = document.getElementById(destination_id)
ReactDOM.render(react_element, container, callback)
# Create a component
Hello = React.createClass({
'displayName': 'Hello',
'getInitialState': lambda: {'counter': 0},
'updateCounter': lambda: (this.setState({'counter': this.state['counter']+1})),
'componentDidMount': lambda: (setInterval(this.updateCounter, 1000)),
'render': lambda: h('div', {'className': 'maindiv'},
h('h1', None, 'Hello ', this.props['name']),
h('p', None, 'Lorem ipsum dolor sit ame.'),
h('p', None, 'Counter: ', this.state['counter'])
)
})
# Render the component in a 'container' div
element = React.createElement(Hello, {'name': 'React!'})
render(element, 'container')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment