25
loading...
This website collects cookies to deliver better user experience
React.createElement()
function all of the time. But doing so is a necessity since JSX isn't a very practical option when using Transcrypt. Or perhaps having all of those el()
's littering your code just makes things look a bit messier than you would prefer. Well, I may have another option for you to try out that eliminates all of those createElement()
calls, yet doesn't really change the way you've been writing your Python React code all that much.el('div', {'style': {'padding': '12px'}}, "Hello React!")
Div({style {padding(12.px)} }) {Text("Hello Jetpack!")}
createElement()
.createElement()
calls for generating every React element in a component tree. Well, I'm happy to say that I accomplished both of those goals. And it turns out that it wasn't even that difficult to do (I really do love Python!). createElement()
in the rest of our Python code.createElement()
and returns that as a new function. It looks like this:def react_component(component):
def react_element(props, *children):
return createElement(component, props, *children)
return react_element
props
and zero or more children
. The return value of the new function is just the call to React.createElement()
that is used in the usual way.component
that we pass into this function doesn't even necessarily have to be a function. In fact, it can even be a string. This feature allows us to handle the first part of our challenge in turning React HTML elements into functional components :Div = react_component('div')
'div'
that is passed into the function gets used as the first parameter in the call to createElement()
in the generated function. This is just like we were previously using it in our own Python React code. We then save the return value of the call to react_component('div')
in a variable called Div
which now contains the newly generated wrapped function.def react_component(component):
def react_element(props, *children):
return createElement(component, props, *children)
return react_element
Form = react_component('form')
Label = react_component('label')
Input = react_component('input')
Ol = react_component('ol')
Li = react_component('li')
Button = react_component('button')
Div = react_component('div')
Span = react_component('span')
createElement()
every time ourselves.createElement()
.from pyreact import useState, render, react_component
from pyreact import Form, Label, Input, Ol, Li
@react_component
def ListItems(props):
items = props['items']
return [Li({'key': item}, item) for item in items]
def App():
newItem, setNewItem = useState("")
items, setItems = useState([])
def handleSubmit(event):
event.preventDefault()
# setItems(items.__add__(newItem))
setItems(items + [newItem]) # __:opov
setNewItem("")
def handleChange(event):
target = event['target']
setNewItem(target['value'])
return Form({'onSubmit': handleSubmit},
Label({'htmlFor': 'newItem'}, "New Item: "),
Input({'id': 'newItem',
'onChange': handleChange,
'value': newItem
}
),
Input({'type': 'submit'}),
Ol(None,
ListItems({'items': items})
)
)
render(App, None, 'root')
createElement()
in sight! Removing all of those el()
's from the original version of app.py has cleaned up the code quite a bit.from pyreact import Form, Label, Input, Ol, Li
import
line above that one, we also imported the new react_component()
function that we created in the pyreact.py module. Here, we now use this as a decorator for any React functional components that we create. When we do, they will also get wrapped by the call to createElement()
when the component gets rendered by React.@react_component
def ListItems(props):
items = props['items']
return [Li({'key': item}, item) for item in items]
createElement()
. So instead of usingel('li', {'key': item}, item)
Li({'key': item}, item)
createElement()
were replaced by their functional component counterparts. All of the props and the child component structure aspects remained exactly the same as they were before making the changes.React.createElement()
when aliased as el()
in my Python code is but a small concession for not having JSX available to use in the Python modules. In all honesty, it never really bothered me all that much. But for larger components, having all of those el()
's cluttering up the Python code can tend to impact readability a bit. By encapsulating the call to createElement()
in the component itself, we can avoid having to explicitly call it in our Python code when building the element tree.createElement()
is that it might not be quite as evident as to what parts of your Python code are creating React elements versus just making regular function calls. Another possible downside might be that this encapsulation of the element creation could be seen as moving away from the mental model of how React actually works. These points are both very subjective, so you will have to decide for yourself if it makes sense to use what I've described here.createElement()
code clutter, I feel that this approach makes it even less of an issue that we are not using JSX for creating React applications. Additionally, by using defined functions instead of strings for creating HTML components, the IDE is able to help us out a little bit better when coding.Source Code:
https://github.com/JennaSys/rtp_demo/tree/no-el
Original Source Code:
https://github.com/JennaSys/rtp_demo
Transcrypt Site:
https://www.transcrypt.org
Jetpack Compose for Web:
https://compose-web.ui.pages.jetbrains.team
Creating React Applications with Python tutorial:
https://leanpub.com/rtptutorial
React to Python Book:
https://pyreact.com