Skip to content

Commit 6d2fddb

Browse files
fix(helper): effects are not cleaned up in @storyblok/field-plugin/react (#417)
## What? In `@storyblok/field-plugin/react`, the effects are not being cleaned up, because the cleanup function is not returned from the `useEffect` callback. I also don't see any cleanup in the Vue 3 helper, but I am not addressing this in this PR. (SHAPE-7195) ## Why? JIRA: SHAPE-7192 Effects must be cleaned up. ## How to test? (optional) The demo app uses `@storyblok/field-plugin/react`: 1. Run `yarn workspace demo dev` 2. Open the sandbox https://plugin-sandbox.storyblok.com/field-plugin?url=http%3A%2F%2Flocalhost%3A8080%2F The app should work normally. You don't normally notice the lack of cleanup, but in local development mode, you'd see the effects run at least twice, and possibly even more if you're modifying the helper itself.
1 parent a90d0b5 commit 6d2fddb

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

packages/lib-helpers/react/src/useFieldPlugin.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,27 @@ export const useFieldPlugin = <Content>(
1212
type: 'loading',
1313
})
1414

15-
useEffect(() => {
16-
createFieldPlugin<Content>({
17-
...options,
18-
onUpdateState: (state) => {
19-
if (state.type === 'error') {
20-
setPlugin({
21-
type: 'error',
22-
error: state.error,
23-
})
24-
} else if (state.type === 'loaded') {
25-
setPlugin({
26-
type: 'loaded',
27-
data: state.data,
28-
actions: state.actions,
29-
})
30-
}
31-
},
32-
})
33-
}, [])
15+
useEffect(
16+
() =>
17+
createFieldPlugin<Content>({
18+
...options,
19+
onUpdateState: (state) => {
20+
if (state.type === 'error') {
21+
setPlugin({
22+
type: 'error',
23+
error: state.error,
24+
})
25+
} else if (state.type === 'loaded') {
26+
setPlugin({
27+
type: 'loaded',
28+
data: state.data,
29+
actions: state.actions,
30+
})
31+
}
32+
},
33+
}),
34+
[],
35+
)
3436

3537
return plugin
3638
}

0 commit comments

Comments
 (0)