How do I create pinterest like web app? #6142
Replies: 1 comment
-
|
You can absolutely build a Pinterest-style layout in Reflex. Here is a practical approach: Masonry layout with CSS columns Reflex supports raw CSS, so you can use CSS multi-column layout which gives you a masonry effect without any JS dependency: import reflex as rx
class State(rx.State):
items: list[dict] = [] # your cards data
page: int = 0
loading: bool = False
def load_more(self):
self.loading = True
# fetch next batch from your data source
new_items = fetch_items(offset=self.page * 20, limit=20)
self.items.extend(new_items)
self.page += 1
self.loading = False
def card(item: dict) -> rx.Component:
return rx.box(
rx.image(src=item["url"], loading="lazy", width="100%"),
rx.text(item["title"]),
style={
"break-inside": "avoid",
"margin-bottom": "1rem",
"border-radius": "8px",
"overflow": "hidden",
},
)
def index() -> rx.Component:
return rx.box(
rx.foreach(State.items, card),
style={
"column-count": "4",
"column-gap": "1rem",
"padding": "1rem",
},
)Lazy loading Use the native Infinite scroll For infinite scroll, use an rx.cond(
State.loading,
rx.spinner(),
rx.button("Load more", on_click=State.load_more),
)If you really want automatic infinite scroll, you can wrap a React infinite scroll component. Reflex lets you wrap any React component: class InfiniteScroll(rx.Component):
library = "react-infinite-scroll-component"
tag = "InfiniteScroll"
data_length: rx.Var[int]
has_more: rx.Var[bool]
# ... map the props you needVideo/GIF support For videos, use Responsive columns Make the column count responsive with media queries in your style dict or use Reflex's responsive props. This approach keeps things lightweight — no heavy JS masonry libraries needed. CSS columns handle the layout, native lazy loading handles performance, and Reflex state handles pagination. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Here are are features I want to implement:
I am currently looking at react packages like masonry or infinitegrid but I want to know if anyone already has a solution I could use that meets all or most of the requirements I described?
Beta Was this translation helpful? Give feedback.
All reactions