Skip to content

@augmentcode/ag-redux-toolkit

Redux + Saga state management for Svelte apps — designed for AI agents. Includes runtime TypeScript source, AI skills, and reference documentation for robust, observable, high-performance state management.

react/migration/side-effects

sub-skill
114 linesSource

>-

React side-effect migration

Shared, persistent, or async React side effects move to sagas. DOM-only effectsthat exist solely to manage one component's mounted DOM can remain local.

React sources include useEffect fetches, subscriptions, timers, debounces,storage sync, IPC/websocket listeners, and custom hooks that hide async work.Svelte $effect is not a React source primitive.

Before: component-owned async effect

tsx
import * as React from "react";
import { useUserContext } from "./UserProvider";

export function UserLoader({ userId }: { userId: string }) {
  const { setUsername } = useUserContext();
  React.useEffect(() => {
    let cancelled = false;
    fetch(`/api/users/${userId}`).then((res) => res.json()).then((data) => {
      if (!cancelled) setUsername(data.name);
    });
    return () => { cancelled = true; };
  }, [setUsername, userId]);
  return null;
}

After: action-triggered saga

ts
import { call, put, takeLatest } from "typed-redux-saga";
import { createAction } from "@augmentcode/ag-redux-toolkit/utils/store/create-action";
import { setUsername } from "../users-slice";

type UserResponse = { name: string };

export const loadUser = createAction<[userId: string]>("users/loadUser");

function fetchUser(userId: string) {
  return fetch(`/api/users/${userId}`).then((response) => response.json() as Promise<UserResponse>);
}

function* loadUserWorker(action: ReturnType<typeof loadUser>) {
  const user = yield* call(fetchUser, action.payload[0]);
  yield* put(setUsername(user.name));
}

export function* usersSaga() {
  yield* takeLatest(loadUser, loadUserWorker);
}

Start the saga from ReactStore setup

ts
import { reactStore } from "../react-store";
import { usersSaga } from "./users-saga";

const disposeStore = reactStore.init();
const cancelUsersSaga = reactStore.runSaga(usersSaga);

export function disposeRuntime() {
  cancelUsersSaga();
  disposeStore();
}

Conversion recipes

React source patternSaga target
useEffect(() => fetch(...), [id])Action + takeLatest(action, worker) + call + put
Component debounce with setTimeoutAction + takeLatest(action, worker) + delay
Reconnect on derived state changeSelector-channel helper from saga code
localStorage sync in component/hookSaga persistence helper called from takeEvery
WebSocket/DOM/IPC subscription shared across appChannel setup in saga with cleanup in finally
Async success/failure local statecreateAsyncAction flow handled inside saga worker

Rules

  • Keep reducers pure; never move React effects into reducers.
  • Use takeLatest for stale-response-prone fetch/search flows.
  • Use takeEvery when every action must be processed.
  • Use selector .effect(...args) in sagas when the saga needs current derivedstate.
  • Do not keep both a migrated useEffect and a saga for the same trigger.
  • Do not use selector .useValue(...args) or direct React signals from saga code.

Bad: duplicate ownership

tsx
// BAD: this duplicates the saga that also persists settingsSaved.
React.useEffect(() => {
  localStorage.setItem("settings", JSON.stringify(settings));
}, [settings]);

Cross-references

  • ../../../core/sagas/SKILL.md — saga patterns and Store-first startup.
  • ../../../core/selector-channels/SKILL.md — reacting to selector value changes from sagas.
  • ../../selectors/SKILL.md — .effect(...args) and non-React saga reads.