Skip to content

Commit 77db34c

Browse files
Merge pull request #22 from effector/fix-wrong-set-state-warning
Fix "setState during render" warning
2 parents 0190f41 + 8d72c90 commit 77db34c

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

apps/playground-app/src/shared/app/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const pageStarted = createEvent<{
99

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

1515
export function declarePage<Ctx = void>(config: {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"build:code": "vite build",
2525
"build:types": "tsc",
2626
"build:directive": "node ./scripts/inject-use-client.mjs",
27-
"build": "pnpm build:code && pnpm build:types && pnpm build:directive"
27+
"build": "pnpm build:code && pnpm build:types && pnpm build:directive",
28+
"dev:playground-app": "cd apps/playground-app && pnpm dev"
2829
},
2930
"devDependencies": {
3031
"@babel/core": "^7.21.4",

src/get-scope.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,19 @@ function HACK_updateScopeRefs(tscope: Scope, values: Values) {
116116
}
117117

118118
/**
119-
* Run scheduled watchers
119+
* Delay links run to separate task,
120+
* so React won't agro on us for updating state during render
121+
*/
122+
queueMicrotask(() => {
123+
HACK_runScopeWatchers(scope, linksToRun);
124+
});
125+
}
126+
127+
function HACK_runScopeWatchers(scope: ScopeInternal, linksToRun: string[]) {
128+
/**
129+
* Run watchers (`useUnit`, etc) to push new values to them
130+
*
131+
* Manual lauch is required because top-down re-render stops at `memo`-ed components
120132
*/
121133
if (linksToRun.length) {
122134
linksToRun.forEach((nodeId) => {

0 commit comments

Comments
 (0)