Skip to content

Commit

Permalink
fix: use_state docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko committed Mar 8, 2024
1 parent d214dfa commit 84d7ca0
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions reacton/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,14 +770,42 @@ def get_widget(el: Element):


def use_state(initial: T, key: str = None, eq: Callable[[Any, Any], bool] = None) -> Tuple[T, Callable[[Union[T, Callable[[T], T]]], None]]:
"""Returns a (value, setter) tuple that is used to manage state in a component.
"""Returns a `(value, setter)` tuple that is used to manage state in a component.
This function can only be called from a component function.
The value returns the current state (which equals initial at the first render call).
Or the value that was last
The value returns the current state (which equals `initial` at the first render call). Or the value that was last set using the setter.
Note that the setter function can be used in two ways.
Directly setting the value:
```python
@reacton.component
def ButtonClick():
clicks, set_clicks = reacton.use_state(0)
def my_click_handler():
set_clicks(clicks+1)
button = w.Button(description=f"Clicked {clicks} times",
on_click=my_click_handler)
return button
```
Updating the value based on the previous/current value.
```python
@reacton.component
def ButtonClick():
clicks, set_clicks = reacton.use_state(0)
def my_click_handler():
set_clicks(lambda prev: prev+1)
button = w.Button(description=f"Clicked {clicks} times",
on_click=my_click_handler)
return button
```
The last one avoid issues with stale data, which means you have a reference to the value of an old render pass (not present in this simple example).
Subsequent
"""
rc = _get_render_context()
return rc.use_state(initial, key, eq)
Expand Down

0 comments on commit 84d7ca0

Please sign in to comment.