Skip to content

Commit

Permalink
fix(helper): effects are not cleaned up in @storyblok/field-plugin/re…
Browse files Browse the repository at this point in the history
…act (#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.
  • Loading branch information
johannes-lindgren authored Oct 4, 2024
1 parent a90d0b5 commit 6d2fddb
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions packages/lib-helpers/react/src/useFieldPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@ export const useFieldPlugin = <Content>(
type: 'loading',
})

useEffect(() => {
createFieldPlugin<Content>({
...options,
onUpdateState: (state) => {
if (state.type === 'error') {
setPlugin({
type: 'error',
error: state.error,
})
} else if (state.type === 'loaded') {
setPlugin({
type: 'loaded',
data: state.data,
actions: state.actions,
})
}
},
})
}, [])
useEffect(
() =>
createFieldPlugin<Content>({
...options,
onUpdateState: (state) => {
if (state.type === 'error') {
setPlugin({
type: 'error',
error: state.error,
})
} else if (state.type === 'loaded') {
setPlugin({
type: 'loaded',
data: state.data,
actions: state.actions,
})
}
},
}),
[],
)

return plugin
}

0 comments on commit 6d2fddb

Please sign in to comment.