Effector.dev Documentation
---
# FAQ
## FAQ
### Why do we need babel/swc plugin for SSR?
Effector plugins inserts special tags - SIDs - into the code, it help to automate serialization and deserialization of stores, so users doesn't have to think about it. See article about sids for more info.
### Why do we need to give names to events, effects etc. ?
This will help in the future, in the development of the effector devtools, and now it is used in the [playground](https://share.effector.dev) on the left sidebar.
If you don't want to do it, you can use the [babel plugin](https://www.npmjs.com/package/@effector/babel-plugin). It will automatically generate the name for events and effects from the variable name.
# Isolated scopes
import Tabs from "@components/Tabs/Tabs.astro";
import TabItem from "@components/Tabs/TabItem.astro";
import SideBySide from "@components/SideBySide/SideBySide.astro";
## Isolated scopes
With scopes you can work with isolated instance for the entire application, which contains an independent clone of all units (including connections between them) and basic methods to access them:
```ts "fork" "allSettled"
import { fork, allSettled } from "effector";
// create a new scope
const scope = fork();
const $counter = scope.createStore(0);
const increment = scope.createEvent();
$counter.on(increment, (state) => state + 1);
// trigger the event and wait for the entire chain to complete
await allSettled(increment, { scope });
console.log(scope.getState($counter)); // 1
console.log($counter.getState()); // 0 - the original store remains unchanged
```
Using fork, we create a new scope, and with allSettled we run a chain of events and effects inside the specified scope and wait for it to complete.
> INFO Scope Independence:
>
> There is no mechanism for sharing data between scopes; each instance is fully isolated and operates independently.
### Why do we need a scope?
In effector, all state is stored globally. In a client-side application (SPA), this is not a problem: each user gets their own instance of the code and works with their own state. But with server-side rendering (SSR) or parallel testing, global state becomes a problem: data from one request or test can “leak” into another. That’s why we need a scope.
* **SSR** — the server runs as a single process and serves requests from many users. For each request, you can create a scope that isolates data from effector’s global scope and prevents one user’s state from leaking into another user’s request.
* **Testing** — when running tests in parallel, data races and state collisions may occur. A scope allows each test to run with its own isolated state.
We provide detailed guides on working with server-side rendering (SSR) and testing. Here, we’ll focus on the core principles of using scopes, their rules, and how to avoid common mistakes.
### Rules for working with scopes
To ensure scopes work correctly, there are a few rules to prevent scope loss:
#### Effect and promise calls
For effect handlers that call other effects, ensure to only call effects, not common asynchronous functions. Furthermore, effect calls should be awaited.
Imperative calls of effects are safe because effector remembers the scope in which the imperative call of the effect began and restores it after the call, allowing for another call in sequence.
You can call methods like `Promise.all([fx1(), fx2()])` and others from the standard JavaScript API because in these cases, the calls to effects still happen synchronously, and the scope is safely preserved.
Count: {count}
> ); }; const myScope = fork({ values: [[$count, 42]], }); render({text}
} ``` # effector-react/compat ```ts import {} from "effector-react/compat"; ``` The library provides a separate module with compatibility up to IE11 and Chrome 47 (browser for Smart TV devices). > WARNING Bundler, Not Transpiler: > > Since third-party libraries can import `effector-react` directly, you **should not** use transpilers like Babel to replace `effector-react` with `effector-react/compat` in your code because by default, Babel will not transform third-party code. > > **Use a bundler instead**, as it will replace `effector-react` with `effector-react/compat` in all modules, including those from third parties. Since `effector-react` uses `effector` under the hood, you need to use the compat-version of `effector` as well. Please, read effector/compat for details. ### Required Polyfills You need to install polyfills for these objects: * `Promise` * `Object.assign` * `Array.prototype.flat` * `Map` * `Set` In most cases, a bundler can automatically add polyfills. #### ViteCount: {count}
> ); }; const scope = fork(); ReactDOM.render(Count: {counter}
> ); }; const scope = fork(); ReactDOM.render(Count: {count}
> ); }; const scope = fork(); render( () => (Count: {count}
> ); }; const scope = fork(); render( () => ({text}
} ``` # useGate ```ts import { useGate } from "effector-solid"; ``` Function for passing data to . ## Methods ### `useGate(Gate, props)` #### Formulae ```ts useGate(Gate: GateCount: {count()}
> ); }; const scope = fork(); render( () => (Count: {count()}
> ); }; const scope = fork(); render( () => ({{ user.name }}
...Count: {{ count }}
``` #### `useUnit(shape)` ##### Arguments 1. `shape` Object or array of ( or or ): Every unit will be processed by `useUnit` and returned as a reactive value in case of or as a function to pass to event handlers in case of or . ##### Returns (Object or Array): * if or : functions with the same names or keys as argument to pass to event handlers. Will trigger given unit in current . * if : reactive value of given with the same names or keys as argument. ##### Examples ###### Basic Usage ```js // model.js import { createEvent, createStore, fork } from "effector"; const incremented = createEvent(); const $count = createStore(0); $count.on(incremented, (count) => count + 1); ``` ```html // App.vueCount: {{ count }}