forked from reactive-python/reactpy-django
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse_query_refetch.py
More file actions
26 lines (18 loc) · 766 Bytes
/
use_query_refetch.py
File metadata and controls
26 lines (18 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from channels.db import database_sync_to_async
from reactpy import component, html
from example.models import TodoItem
from reactpy_django.hooks import use_query
async def get_items():
return await database_sync_to_async(TodoItem.objects.all)()
@component
def todo_list():
item_query = use_query(get_items)
if item_query.loading:
rendered_items = html.h2("Loading...")
elif item_query.error or not item_query.data:
rendered_items = html.h2("Error when loading!")
else:
rendered_items = html.ul([html.li(item.text, key=item.pk) for item in item_query.data])
def on_click(event):
item_query.refetch()
return html.div("Rendered items: ", rendered_items, html.button({"onClick": on_click}, "Refresh"))