question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Addon Actions is not handling React events properly

See original GitHub issue

Describe the bug Attempting to use action from @storybook/addon-actions causes console errors and the output doesn’t match the docs, possibly due to how the React Synthetic Events are being serialized.

To Reproduce

create-react-app sbtemp
cd sbtemp
npx -p @storybook/cli sb init
yarn storybook

Then the demo button in the “Button” “with Text” story is clicked. Google Chrome gives a deprecation warning:

[Deprecation] 'window.webkitStorageInfo' is deprecated. Please use 'navigator.webkitTemporaryStorage' or 'navigator.webkitPersistentStorage' instead.

The output in the Storybook Actions addon panel doesn’t match the output shown in the docs, reading as clicked: [Class] instead of the clicked: ["[SyntheticEvent]", null, "[SyntheticEvent]"] shown on https://storybook.js.org/docs/addons/introduction/

clicked: [Class]
0: Class
_dispatchInstances: Object
_dispatchListeners: function action()
_targetInst: Object
altKey: false
bubbles: true
button: 0
buttons: 0
cancelable: true
clientX: 82
clientY: 32
ctrlKey: false
currentTarget: HTMLButtonElement
defaultPrevented: false
detail: 1
dispatchConfig: Object
eventPhase: 3
getModifierState: function modifierStateGetter()
isDefaultPrevented: function functionThatReturnsFalse()
isPropagationStopped: function functionThatReturnsFalse()
isTrusted: true
metaKey: false
movementX: 0
movementY: 0
nativeEvent: MouseEvent
pageX: 82
pageY: 32
relatedTarget: null
screenX: 292
screenY: 185
shiftKey: false
target: HTMLButtonElement
timeStamp: 266833.9550000001
type: "click"
view: Window

Additionally, if react and react-dom is downgraded to 16.3.2 (the version I was using when I encountered the issue), there is also another console error:

Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're adding a new property in the synthetic event object. The property is never released. See https://fb.me/react-event-pooling for more information.

Expected behavior No console errors or warnings, and for the Action panel to match the docs.

System:

Additional context I was unable to recreate this additional error, but in my own setup I also saw telejson / JSON.stringify choke on something (Date-related?) with an error of Uncaught TypeError: toISOString is not a function

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:14
  • Comments:33 (18 by maintainers)

github_iconTop GitHub Comments

13reactions
petermikitshcommented, Aug 13, 2020

This is my solution to action logging concerning React synthetic events.

The key is to set view to undefined, because it’s a reference to window, and logging window substantially slows down the performance of storybook (because window is giant-- that’s by no means Storybook’s fault or anything).

Use this facade for Storybook’s action function throughout your project:

import { action } from '@storybook/addon-actions';

/* Partial event logging, as full logging can be expensive/slow
 * Invocation: partialLog('actionName')(eventObj, ...args)
 */
export const partialAction = (actionName) => {
  const beacon = action(actionName);
  return (eventObj, ...args) => {
    beacon({ ...eventObj, view: undefined }, ...args);
  };
};

Literal example in a story:

const Template: Story<AccordionProps> = (args) => <Accordion {...args} />;

export const Simple = Template.bind({});
Simple.args = {
  header: 'Hello World',
  body: 'This is my example accordion.',
  onChange: partialAction('onChange'), // fires whenever my accordion expands or collapses
};

This fix gets rid of React error message:

Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the method `isDefaultPrevented` on a released/nullified synthetic event. This is a no-op function. If you must keep the original synthetic event around, use event.persist(). See https://fb.me/react-event-pooling for more information.

It’s a good compromise of logging nearly all the data (except the ‘view’ property), and keeping Storybook fast.

4reactions
wonskarolcommented, Apr 25, 2019

Im facing similar issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Storybook: event does not show in the action panel
I'm trying to create the click event on my storybook, but It doesn't matter what I do the event does not appear in...
Read more >
Actions - Storybook - JS.ORG
The actions addon is used to display data received by event handler (callback) arguments in your stories. Action args. Actions work via supplying...
Read more >
How to do interaction testing with React 18 and Storybook
These tests simulate user actions within the application, such as interacting with the components by clicking buttons or typing into input ...
Read more >
Essential Addons
Actions is an addon that helps visualize when components emit events. If your component has functions like onClick , onDrag , onSubmit ...
Read more >
React interactivity: Events and state - Learn web development
Objective: To learn about handling events and state in React, ... The camel-cased nature of onClick is important — JSX will not recognize ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found