When wrapping a React component, you may need to define custom code or hooks that are specific to the component. This is done by defining the add_custom_code
or add_hooks
methods in your component class.
Custom Code
Custom code is any JS code that need to be included in your page, but not necessarily in the component itself. This can include things like CSS styles, JS libraries, or any other code that needs to be included in the page.
The above example will render the following JS code in the page:
Custom Hooks
Custom hooks are any hooks that need to be included in your component. This can include things like useEffect
, useState
, or any other hooks from the library you are wrapping.
- Simple hooks can be added as strings.
- More complex hooks that need to have special import or be written in a specific order can be added as
rx.Var
with aVarData
object to specify the position of the hook.- The
imports
attribute of theVarData
object can be used to specify any imports that need to be included in the component. - The
position
attribute of theVarData
object can be set toHooks.HookPosition.PRE_TRIGGER
orHooks.HookPosition.POST_TRIGGER
to specify the position of the hook in the component.
- The
The ComponentWithHooks
will be rendered in the component in the following way:
You can mix custom code and hooks in the same component. Hooks can access a variable defined in the custom code, but custom code cannot access a variable defined in a hook.