widgetti / reacton

A pure Python port of React for ipywidgets
https://reacton.solara.dev/
MIT License
296 stars 18 forks source link

feat: Fragment support to have multiple children in a component #32

Closed maartenbreddels closed 7 months ago

maartenbreddels commented 7 months ago

Similar to React, where children in a Fragment will be 'extended' to a parent list (like children) instead of 'appended'

Example

import reacton
import reacton.ipywidgets as w

@reacton.component
def Children():
    with reacton.Fragment():  # option for reacton, mandatory for solara
        w.Button(description="1")
        w.Button(description="2")

@reacton.component
def Test():
    with w.VBox():
        Children()
Test()

Since solara overrides the '_default_container', this is mandatory to use with solara (even when just imported). For solara v2 we will probably also start using fragments.

maartenbreddels commented 7 months ago

To make this work in solara, use:

import solara # this changes reacton.core. _default_container
import reacton.core
# now restore it
reacton.core. _default_container = reacton.Fragment
maartenbreddels commented 7 months ago

Note, Fragment does not have to be used explicitly.

import reacton
import reacton.ipywidgets as w

@reacton.component
def Children():
    # will use fragment implicitly, because we have 2 elements
    w.Button(description="1")
    w.Button(description="2")

@reacton.component
def Test():
    with w.VBox():
        Children()
Test()