跳至主要內容

syncEffect(...)

取得 atom 效果 的函數,適用於 recoil-sync 函式庫,以使用 <RecoilSync> 元件,同步 Atom 與使用外部定義的狀態。


function syncEffect<T>(options: {
refine: Checker<T>,

itemKey?: string,
storeKey?: string,

syncDefault?: boolean,

// Optional for advanced mappings
read?: ReadAtom,
write?: WriteAtom<T>,
}): AtomEffect<T>

選用選項

  • itemKey - 外部儲存體中,此特定 atom 的字串鍵。若未提供,則預設為 atom 自身的鍵。
  • storeKey - 將此效果與要同步的 <RecoilSync> 儲存體搭配的字串鍵。
  • syncDefault - 如果為 true,則 atom 會同步實際的預設值,而不是清除或重設外部狀態。如果設定此選項,則也會在首次讀取 atom 時嘗試寫入預設值,而不只是在設定時。
  • read - 描述如何從外部儲存體讀取這個 atom 的選用回呼。
  • write - 描述如何將這個 atom 寫入外部儲存體的選用回呼。

範例

有關範例,請參閱 同步效果指南

進階對應

read 介面

type ReadItem = ItemKey =>
| void
| DefaultValue
| Promise<DefaultValue | mixed>
| Loadable<DefaultValue | mixed>
| mixed;

type ReadAtom = ({read: ReadItem}) =>
| DefaultValue
| Promise<DefaultValue | mixed>
| Loadable<DefaultValue | mixed>
| mixed;

write 介面

type WriteItem = <T>(ItemKey, DefaultValue | T) => void;
type ResetItem = ItemKey => void;

type WriteAtomInterface = {
write: WriteItem,
reset: ResetItem,
read: ReadItem,
};
type WriteAtom<T> = (WriteAtomInterface, DefaultValue | T) => void;

請參閱 這些範例