跳至主要內容

useGotoRecoilSnapshot(snapshot)

此 hook 會傳回一個 callback,它會將 Snapshot 當作參數,並將目前的 <RecoilRoot> 狀態更新為與這個 atom 狀態相符。

function useGotoRecoilSnapshot(): Snapshot => void

交易範例

重要注意事項:這個範例效率不佳,因為它會讓元件訂閱 所有 狀態變更以重新渲染。

function TransactionButton(): React.Node {
const snapshot = useRecoilSnapshot(); // Subscribe to all state changes
const modifiedSnapshot = snapshot.map(({set}) => {
set(atomA, x => x + 1);
set(atomB, x => x * 2);
});
const gotoSnapshot = useGotoRecoilSnapshot();
return <button onClick={() => gotoSnapshot(modifiedSnapshot)}>Perform Transaction</button>;
}

時光旅行範例

請參閱 時光旅行範例