Skip to content

Commit

Permalink
make state persisted with useAddonState
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Jul 4, 2023
1 parent 65f3695 commit 7721371
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions code/addons/actions/src/manager.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import React, { useState } from 'react';
import { addons, types, useChannel } from '@storybook/manager-api';
import React from 'react';
import { addons, types, useAddonState, useChannel } from '@storybook/manager-api';
import { STORY_CHANGED } from '@storybook/core-events';
import { Badge, Spaced } from '@storybook/components';
import ActionLogger from './containers/ActionLogger';
import { ADDON_ID, CLEAR_ID, EVENT_ID, PANEL_ID, PARAM_KEY } from './constants';

function Title() {
const [count, setCount] = useState(0);
const [{ count }, setCount] = useAddonState(ADDON_ID, { count: 0 });

useChannel({
[EVENT_ID]: () => {
setCount((c) => c + 1);
setCount((c) => ({ ...c, count: c.count + 1 }));

This comment has been minimized.

Copy link
@prewk

prewk Aug 25, 2023

Sometimes c.count (and count further up from useAddonState) is undefined, which cause a run-time error.

image image

Triggers every time at the end when I run this line in an interaction test:

await userEvent.click(canvas.getByTestId('item-2'));

🤷 (Storybook 7.3.2)

This comment has been minimized.

Copy link
@ndelangen

ndelangen Aug 28, 2023

Author Member

This is fixed by #23804

},
[STORY_CHANGED]: () => {
setCount(0);
setCount((c) => ({ ...c, count: 0 }));
},
[CLEAR_ID]: () => {
setCount(0);
setCount((c) => ({ ...c, count: 0 }));
},
});

Expand Down

0 comments on commit 7721371

Please sign in to comment.