Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def Inner(shared_count, button_id):
count, set_count = idom.hooks.use_state(shared_count)
async def on_click(event):
set_count(count + 1)
return idom.html.button(
{"onClick": on_click, "id": button_id, "count": count},
f"Current click count: {count}",
)
async def ComponentWithMemo():
location, set_location = idom.hooks.use_state("left")
count = idom.hooks.use_memo(function_to_memoize, location)
async def on_left_button_click(event):
set_location("left")
async def on_right_button_click(event):
set_location("right")
return idom.html.div(
idom.html.button(
{"onClick": on_left_button_click, "id": "left-button"}, "left button"
),
idom.html.button(
{"onClick": on_right_button_click, "id": "right-button"}, "right button"
),
f"Memo trigger count: {count}",
async def Input(message=None):
message, set_message = idom.hooks.use_state(message)
message_var.set(message)
async def on_change(event):
if event["value"] == "this is a test":
set_message(event["value"])
if message is None:
return idom.html.input({"id": "input", "onChange": on_change})
else:
return idom.html.p({"id": "complete"}, ["Complete"])
async def ComponentWithMemo():
location, set_location = idom.hooks.use_state("left")
memoized_func = idom.hooks.use_lru_cache(function_to_memoize, maxsize=2)
last_memoized_location = memoized_func(location)
async def on_left_button_click(event):
set_location("left")
async def on_center_button_click(event):
set_location("center")
async def on_right_button_click(event):
set_location("right")
return idom.html.div(
idom.html.button(
{"onClick": on_left_button_click, "id": "left-button"}, "left button"
),
async def simple_stateful_element():
index, set_index = idom.hooks.use_state(0)
set_index(index + 1)
return idom.html.div(index)
async def Holder(index, shared_current_index):
current_index, set_current_index = idom.hooks.use_state(shared_current_index)
hovered, set_hovered = idom.hooks.use_state(False)
if current_index == index:
state = "filled"
elif hovered:
state = "hover"
else:
state = "empty"
@idom.event(prevent_default=True, stop_propagation=True)
async def on_hover(event):
if not hovered:
set_hovered(True)
async def on_start(event):
set_hovered(True)
set_current_index(None)
async def Holder(index, shared_current_index):
current_index, set_current_index = idom.hooks.use_state(shared_current_index)
hovered, set_hovered = idom.hooks.use_state(False)
if current_index == index:
state = "filled"
elif hovered:
state = "hover"
else:
state = "empty"
@idom.event(prevent_default=True, stop_propagation=True)
async def on_hover(event):
if not hovered:
set_hovered(True)
async def on_start(event):
async def Todo():
items, set_items = idom.hooks.use_state([])
async def add_new_task(event):
if event["key"] == "Enter":
set_items(items + [event["value"]])
tasks = []
for index, text in enumerate(items):
async def remove_task(event, index=index):
set_items(items[:index] + items[index + 1 :])
task_text = idom.html.td(idom.html.p(text))
delete_button = idom.html.td({"onClick": remove_task}, idom.html.button(["x"]))
tasks.append(idom.html.tr(task_text, delete_button))