Bu sahifa hali tarjima qilinmagan

Tarjima qoshish uchun havola boyicha o'tib Pull Request oching (havolaga o'tish).

Standart til uchun tarkibni ko'rsatadi.

Troubleshooting Effector

Common Errors

store: undefined is used to skip updates. To allow undefined as a value provide explicit {Ā skipVoid:Ā falseĀ } option

This error indicates that you are trying to pass undefined as a value to your store, which might not be the intended behavior.

If you really need to store undefined, pass an object with {Ā skipVoid:Ā falseĀ } as the second argument to createStore:

const $store = createStore(0, {
  skipVoid: false,
});

serialize: One or more stores dont have sids, their values are omitted

Before version 23.3.0

Before version 23.3.0, this error was also known as: There is a store without sid in this scope, its value is omitted.

This error commonly occurs in SSR scenarios due to the absence of an sid (stable id), which is required for proper hydration of store data from the server to the client.

To fix this, add an sid to your store. You can do this in one of the following ways:

  1. Use the Babel or SWC plugin to handle it automatically.

  2. Manually specify an sid by providing an object with a sid property as the second argument to createStore:

    const $store = createStore(0, {
      sid: "unique id",
    });
    

For more details, see Understanding sid and its purpose.

scopeBind: scope not found

This error occurs when a scope is lost at some point in execution, preventing scopeBind from associating an event or effect with the correct execution scope.
It may be caused by:

  1. Using a ā€œscope-freeā€ mode where scopes are not present in your application.
  2. Calling units outside of a scope.

Possible Solutions:

  1. Ensure scopeBind is used within effects:

    const event = createEvent();
    
    // āŒ - Do not call scopeBind inside callbacks
    const effectFx = createEffect(() => {
      setTimeout(() => {
        scopeBind(event)();
      }, 1111);
    });
    
    // āœ… - Use scopeBind inside the effect
    const effectFx = createEffect(() => {
      const scopeEvent = scopeBind(event);
    
      setTimeout(() => {
        scopeEvent();
      }, 1111);
    });
    
  2. Ensure that your units are used inside a scope:

    • When working with a framework, use useUnit.
    • If calling an event or effect outside a framework, use allSettled and provide the appropriate scope as an argument.

If necessary, and you want to suppress the error, you can pass {Ā safe:Ā trueĀ } as an option:

const scopeEvent = scopeBind(event, {
  safe: true,
});

Gotchas

sample.fn does not narrow the type passed from sample.filter

A common type-related issue with sample occurs when a check is performed inside filter, but fn does not receive the expected narrowed type.

Fixing this issue.

My state did not change

If your state does not update as expected, you are likely working with scopes and, at some point, the active scope was lost. As a result, your unit executed in the global scope instead.
Find more details about this behavior here.

This issue often occurs when passing units (events or effects) into external function callbacks such as:

  • setTimeout / setInterval
  • addEventListener
  • webSocket, etc.

Solution:

Bind your event or effect to the current scope using scopeBind:

const event = createEvent();

// āŒ - This will execute the event in the global scope
const effectFx = createEffect(() => {
  setTimeout(() => {
    event();
  }, 1000);
});

// āœ… - This ensures the event executes in the correct scope
const effectFx = createEffect(() => {
  const scopeEvent = scopeBind(event);
  setTimeout(() => {
    scopeEvent();
  }, 1000);
});

Using units without useUnit

If youā€™re using events or effects in a framework without useUnit, this may also lead to incorrect behavior related to scopes.
To fix this, pass the unit to the useUnit hook and use the returned value:

import { event } from "./model.js";

const Component = () => {
  return <button onClick={() => event()}></button>;
};
Best Practice

What is scope loss and why does it happen.

No Answer to Your Question?

If you couldnā€™t find the answer to your question, you can always ask the community:

Tarjima jamiyat tomonidan qollanilyapti

Ingliz tilidagi hujjatlar eng dolzarb hisoblanadi, chunki u effector guruhi tomonidan yozilgan va yangilanadi. Hujjatlarni boshqa tillarga tarjima qilish jamiyat tomonidan kuch va istaklar mavjud bo'lganda amalga oshiriladi.

Esda tutingki, tarjima qilingan maqolalar yangilanmasligi mumkin, shuning uchun eng aniq va dolzarb ma'lumot uchun hujjatlarning asl inglizcha versiyasidan foydalanishni tavsiya etamiz.

Hammualliflar