Skip to content

Commit

Permalink
Merge pull request #22 from effector/fix-wrong-set-state-warning
Browse files Browse the repository at this point in the history
Fix "setState during render" warning
  • Loading branch information
AlexandrHoroshih authored Sep 16, 2023
2 parents 0190f41 + 8d72c90 commit 77db34c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/playground-app/src/shared/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const pageStarted = createEvent<{

export const $currentPage = createStore<(typeof pageTypes)[number] | null>(null).on(
pageStarted,
(_, { pageType }) => pageType
(_, { pageType }) => pageType ?? "unknown_page"
);

export function declarePage<Ctx = void>(config: {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"build:code": "vite build",
"build:types": "tsc",
"build:directive": "node ./scripts/inject-use-client.mjs",
"build": "pnpm build:code && pnpm build:types && pnpm build:directive"
"build": "pnpm build:code && pnpm build:types && pnpm build:directive",
"dev:playground-app": "cd apps/playground-app && pnpm dev"
},
"devDependencies": {
"@babel/core": "^7.21.4",
Expand Down
14 changes: 13 additions & 1 deletion src/get-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,19 @@ function HACK_updateScopeRefs(tscope: Scope, values: Values) {
}

/**
* Run scheduled watchers
* Delay links run to separate task,
* so React won't agro on us for updating state during render
*/
queueMicrotask(() => {
HACK_runScopeWatchers(scope, linksToRun);
});
}

function HACK_runScopeWatchers(scope: ScopeInternal, linksToRun: string[]) {
/**
* Run watchers (`useUnit`, etc) to push new values to them
*
* Manual lauch is required because top-down re-render stops at `memo`-ed components
*/
if (linksToRun.length) {
linksToRun.forEach((nodeId) => {
Expand Down

0 comments on commit 77db34c

Please sign in to comment.