Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Reactivity in the scope of the webcomponent without the use of context(this)
useProp allows you to work with a prop(property) of the webcomponent in a similar way to useState.
Where :
value: Current value of the prop.
setValue: Callback to update the value of the prop.
myProp: string, defines the name of the prop to be used by the hook.
Where:
useCounter is a customHook and that it can work with any property of the webcomponent of type Number.
useCounter returns 2 methods increment and decrement that modify the value of the prop.
useCounter can be instantiated multiple times for different properties.
Where:
const [state,setState]
: Return of useState
, the arguments allow reading and updating of the state associated with the hook instance.
state
: Current state.
setState
: Current status updater.
useState( optionalInitialState )
: Function that associates the state to the webcomponent:
optionalInitialState
: Optional parameter that defines the initial state associated to the hook instance, If optionalInitialState
is a function it will be executed in order to obtain the initial state only at the moment of the hook instance for the first time.
Hook that creates a reference that curren is the instance of the webcomponent.
Returns the instance of the webcomponent in reference format, this reference allows to extend behaviors when creating customHooks.
From the example we can highlight that useListener is a customHook that allows listening to an event from the webcomponent without the need to link said event to the VirtualDOM.
Improves the experience of reusing logic between webcomponents based on Atomico
Atomico today offers more Hooks external to the core, we invite you to visit @atomico/hooks with more than 50 hooks to enhance the use of webcomponents 😎
Allows to run side effects after rendering
Where :
effectCallback
: Function that is executed one or more times according to optionalArgumentList
,effectCallback
can return a function that will be executed only if effectCallback
is executed again or the webcomponent is unmounted.
optionalArgumentList
: Array of arguments that controls the execution of effectCallback
, if an argument ofoptionalArgumentList
changes it will trigger that effectCallback
is executed again without first cleaning the effects subscribed by the previous execution.
useLayoutEffect replicates the logic of useEffect but with synchronous execution after rendering.
useLayoutEffect replicates the logic of useEffect but with synchronous execution before rendering.
Where :
memoValue
: Return memorized by useMemo.
callback
: Function that is executed one or more times according to optionalArgumentList
.
optionalArgumentList
: Array of arguments that controls the execution of callback
, if an argument of optionalArgumentList
changes it will trigger that callback
is executed again.
Hook that allows you to memorize a callback so that it keeps its scope
Where:
memoCallback
: Return memorized by useCallback.
Easily observe asynchronous processes
The purpose of this hook is to facilitate the consumption of promises.
where:
callback
: asynchronous function.
args
: arguments that callback requires.
autorun
: by default true, it automatically executes the promise, if it is false the execution of the promise is suspended.
promise
: return object, at the type level it defines the following properties:
pending
: boolean, defines if the promise is executed but pending resolution.
fulfilled
: boolean, defines if the promise has been fulfilled
result
: result of the promise.
rejected
: boolean, defines if the promise has been rejected.
Note: When using Typescript Do not force the types for the second argument of usePromise, as usePromise will infer them from your callback.
It is preferable to use ternary or if to condition the reading of states, since this will help the definition of result
Note: At the type level, autocompletion is conditional on the state you are looking to observe.
Allows the host that instantiates this useProvider to become the context.
Force an update, ideal for working with references
Since version Atomico@1.62.0 has introduced a context api as part of the core.
With the new contexts API you will be able to easily communicate components without the need to handle events, by default the communication is top down, but through it you can share anything as long as it is defined as an object.
Atomico's api is similar to React's Context api, let's explore the behavior of Atomico's context api:
create a custom Element as a context, this will serve to synchronize all the components nested within it, you must always remember to declare the tagname of this customElement, example
It allows to consume the return of createContext, let's go back to the previous example and suppose that we want to consume the customElement Theme created by createContext, the code for this would be the following:
In this way useContext captures the value of the parent component or reuses the value given by default to createContext.
By setting the value
prop on the context, you can pass custom values down the sub-tree:
It is highly recommended to always use custom properties to expose the appearance configuration of your component at the static CSS level, since useContext is designed to share information between components in a unidirectional way.
It is ideal to always prioritize a conversation between parent and child through events or props api, but sometimes the depth of the DOM makes this process difficult, for this the context api has been introduced. To remove DOM depth limitations and ensure synchronization based on a unique identifier, some ideal cases to solve with the context api are:
Synchronize states or private methods between components.
Share a value or states inherited from the parent regardless of DOM depth.
Dispatch events from the webcomponent without referencing the context(this)
Where:
dispatchEvent: callback, dispatches the event from the webcomponent and allows defining the detail by receiving a first parameter
myEvent: string, name of the event to dispatch.
eventInit: optional object, event configuration.
The second parameter of useEvent
allows customizing the behavior of the even:
Atomico now introduces a new hook called useAbortController, which allows aborting the execution of asynchronous calls, example:
The idea is to create an instance of AbortController every time the hook's parameters change. Each parameter change will abort the previously generated controller, thus cancelling any subscribed promises.
The significant advantage of using useAbortController is the automatic cancellation of asynchronous processes that depend on it when modifying the arguments provided to useAbortController (similar to useEffect).
suspend the execution of a render until the resolution of an asynchronous process
with a similar approach to React's use hook, but with scope approach.
scope approach? yes, this hook seeks to resolve a promise from a callback return, this allows you to regenerate the promise according to the scope, example:
where:
getUser
: async callback.
[ userId ]
: arguments that if changed regenerate the promise.
user
: promise return
Like useEffect, the promise will be executed every time the arguments to the second parameter of useAsync change.
Rendering will be suspended until the promise is resolved or rejected, the resolution of the promises can be observed with useSuspense
allows to listen to all useAsync executions nested in the component, example: