Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Return multiple components on the same level #1213

Answered by Archmonger
NewBobCon asked this question in Question
Discussion options

I've just started working on making a simple prototype site. Is there any capability to return multiple components on the same level? The reason for doing so is to try and follow semantic html guidelines where the body tag might have a header, main, and footer tag without having to first place it into a div.

<body>
 <header>MyHeader</header>
 <main>The main content</main>
 <footer>MyFooter</footer>
</body>

This is how I would think to do it, but the output is not as expected:

from reactpy import component, html, run
from reactpy.backend.flask import configure
@component
def App():
 return (header(), main(), footer())
@component
def header():
 return html.header("Home", navigation())
@component
def main():
 return html.main("Welcome to my site!")
@component
def navigation():
 return html.nav("Item1", "Item2", "Item3", "Item4", "Item5")
@component
def footer():
 return html.footer("This site was made with ReactPy")
run(App, "127.0.0.1", 8080)

image

There is a way to get this to render, but it requires placing everything in a div:

from reactpy import component, html, run
from reactpy.backend.flask import configure
@component
def App():
 return html._(header(), main(), footer())
@component
def header():
 return html.header("Home", navigation())
@component
def main():
 return html.main("Welcome to my site!")
@component
def navigation():
 return html.nav("Item1", "Item2", "Item3", "Item4", "Item5")
@component
def footer():
 return html.footer("This site was made with ReactPy")
run(App, "127.0.0.1", 8080)

image

Any help or guidance would be great. Thank you in advance!

You must be logged in to vote

For your scenario of

<body>
 <header>MyHeader</header>
 <main>The main content</main>
 <footer>MyFooter</footer>
</body>

If you want to structure it as "multiple components on the same level", your second example is correct.
You need to use a parent HTML element, in this case html._ (a HTML fragment), to "encapsulate" things that are within the same level.

@component
def App():
 return html._(header(), main(), footer())

You can not directly return an iterable as a top-level root node within your component (such as your return (header(), main(), footer()) example.

Replies: 1 comment

Comment options

For your scenario of

<body>
 <header>MyHeader</header>
 <main>The main content</main>
 <footer>MyFooter</footer>
</body>

If you want to structure it as "multiple components on the same level", your second example is correct.
You need to use a parent HTML element, in this case html._ (a HTML fragment), to "encapsulate" things that are within the same level.

@component
def App():
 return html._(header(), main(), footer())

You can not directly return an iterable as a top-level root node within your component (such as your return (header(), main(), footer()) example.

You must be logged in to vote
0 replies
Answer selected by Archmonger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /