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.

Default for function prop overrides action argType

See original GitHub issue

Describe the bug When creating a story for a component that sets empty functions as defaults for (some) event handling props, the default functions get passed in through args, even though the argType for the prop is set to { action: 'actionName'}.

To Reproduce Steps to reproduce the behavior:

  1. Create a component that accepts a function as a prop.
  2. Give the prop a function as default value (e.g. () => {})
  3. Create a story, which sets the argType for the prop to {action: 'actionName'}
  4. Trigger the function in storybook.

Expected behavior A log of the action ‘actionName’ in the Actions tab.

Code snippets TestStory.js:

import React from 'react';

const TestStory = ({ onClick = () => { console.log('test') } }) => <button onClick={onClick}>Click me!</button>;

export default TestStory;

TestStory.stories.js:

import React from 'react';
import TestStory from './TestStory';

export default {
  title: 'core/Test case',
  component: TestStory,
  args: {},
  argTypes: {
    onClick: { action: 'onClick', control: null }
  }
};

export const Default = args => {
  console.log(args.onClick);
  return <TestStory {...args} />;
};

System: Environment Info:

System: OS: macOS 10.15.6 CPU: (16) x64 Intel® Core™ i9-9980HK CPU @ 2.40GHz Binaries: Node: 12.13.1 - ~/.nvm/versions/node/v12.13.1/bin/node Yarn: 1.22.4 - /usr/local/bin/yarn npm: 6.14.7 - ~/.nvm/versions/node/v12.13.1/bin/npm Browsers: Chrome: 84.0.4147.135 Safari: 13.1.2

Additional context If the component is in the same file as the story, the problem doesn’t occur. If storybook is unable to fetch info about the components properties the issue also doesn’t occur: when storybook is unable to prefill the ArgTable in the Controls tab.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
thebinaryfelixcommented, Oct 14, 2020

This worked for me. I’m using Storybook version 6.0.5

export default {
  component: Button,
  title: 'Button',
 // Add the defaultValue property
  argTypes: { onClick: { defaultValue: null, action: 'clicked' } },
};

const Template = (args) => <Button {...args} />;

export const Basic = Template.bind({});

Reference: I had the same problem, but I’ve found a work around based on this test file.

https://github.com/storybookjs/storybook/blob/9b1d2d30f31f54992b1fdd19987bcce47c7c1f71/addons/actions/src/preset/addArgs.test.ts#L61

3reactions
deividmarquescommented, Oct 27, 2020

I used it and it worked

argTypes: {​​​​​​​​ onOpen: {​​​​​​​​ defaultValue: null, }​​​​​​​​, onChange: {​​​​​​​​ defaultValue: null, }​​​​​​​​, }​​​​​​​​
parameters: {
  actions: { argTypesRegex: '^on.*' },
},

md

Read more comments on GitHub >

github_iconTop Results From Across the Web

Default for function prop overrides action argType #12120
Describe the bug When creating a story for a component that sets empty functions as defaults for (some) event handling props, the default...
Read more >
ArgTypes - Storybook - JS.ORG
ArgTypes are a first-class feature in Storybook for specifying the behaviour of Args. By specifying the type of an arg, you constrain the...
Read more >
React Storybook: Trying to set specific actions to different ...
You can override Template.argTypes attribute on the Secondary component and achieve the same behavior.
Read more >
A complete guide to React default props - LogRocket Blog
Cover three ways to implement default props in React and provide default ... A React component is simply a JavaScript function that takes...
Read more >
IgrSpreadsheet | Ignite UI for React - Infragistics
new IgrSpreadsheet(props: IIgrSpreadsheetProps): IgrSpreadsheet. Overrides ... eventArgument="ui.action" argType="string" Gets the action that was executed.
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