Gate Properties

Gate connects a component to the model: the gate is open while the component is mounted and holds its props. For example, a gate can collect all required data when the component mounts.

This allows you to send props back to Store to create feedback loop.

Gate can be used via useGate hook. Gate stores and events can be used in the application as regular units

Gate can have two states:

  • Open, which means mounted
  • Closed, which means unmounted

A gate opens on mount with the props it has at that moment and passes them to .set itself, so .open fires before .set. <KeepAlive> deactivation leaves the gate open, only unmounting closes it. During a server render the gate stays closed, see useGate.

Gate Properties

.state

Important

Do not modify state value! It is derived store and should be in predictable state.

Store<Props>: DerivedStore with current state of the given gate. The state comes from the second argument of useGate.

.open

Important

Do not manually call this event. It is an event that depends on a Gate state.

Event<Props>: Event which will be called during gate mounting, with the current props

.close

Important

Do not manually call this event. It is an event that depends on a Gate state.

Event<Props>: Event which will be called during a gate unmounting.

.status

Important

Do not modify status value! It is derived store and should be in predictable state.

Store<boolean>: Boolean DerivedStore, which show if given gate is mounted.

Example

The model reacts to the gate. A component opens it with useGate.

model.ts
import { createEffect, sample } from "effector";
import { createGate } from "effector-vue/composition";
export const PageGate = createGate<{ id: string }>();
const loadPageFx = createEffect(async ({ id }: { id: string }) => {
const response = await fetch(`/api/pages/${id}`);
return response.json();
});
sample({ clock: PageGate.open, target: loadPageFx });
PageGate.status.watch((opened) => console.log("page opened:", opened));
Contributors