Hooks#
Hooks introduce various features into stateless @component
Elements.
Rules of Hooks#
Edifice Hooks imitate React Hooks, and follow the React Rules of Hooks.
The exact same Hooks must be called
in exactly the same order on every call to a @component
function.
- Only call Hooks
In the top level of a
@component
Element render function.In the body of a Custom Hook.
- Never call Hooks
In a conditional statement.
In a loop.
Base Hooks#
|
Persistent mutable state Hook inside a |
|
Side-effect Hook inside a |
|
Asynchronous side-effect Hook inside a |
Derived Hooks#
Derived Hooks are functions which are written in terms of other Hooks.
These Derived Hooks are provided by Edifice.
|
Hook for creating a |
|
Side-effect Hook for when a |
|
Hook to call an async function from a non-async context. |
|
Hook for a callback function to pass as props. |
Hook to track mouse hovering. |
|
|
This Hook returns a function which will stop the application by calling |
Custom Hooks#
A “Custom Hook” is just a Derived Hook that is defined in user code.
For example, here is a Custom Hook which provides a clock value. Using this
Hook will cause the @component
to re-render every second with the
clock value incremented each time.
def use_clocktick() -> int:
tick, tick_set = use_state(0)
async def increment():
await asyncio.sleep(1)
tick_set(tick + 1)
use_async(increment, tick)
return tick
Use it in a @component
Element like this.
@component
def Clock(self):
tick = use_clocktick()
Label(str(tick))