Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React - Browser error: fn.apply is not a function #8835

Open
tonyjmartinez opened this issue Nov 14, 2019 · 13 comments
Open

React - Browser error: fn.apply is not a function #8835

tonyjmartinez opened this issue Nov 14, 2019 · 13 comments

Comments

@tonyjmartinez
Copy link

tonyjmartinez commented Nov 14, 2019

Describe the bug
Most of my components produce the following error in the browser.
TypeError: fn.apply is not a function

To Reproduce
Steps to reproduce the behavior:

  1. npm run storybook
  2. Error shows up in browser

Expected behavior
No error, show my component

Screenshots
Screen Shot 2019-11-14 at 11 05 09 AM

System:

Environment Info:

  System:
    OS: macOS 10.15.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 10.15.3 - ~/.nvm/versions/node/v10.15.3/bin/node
    Yarn: 1.19.0 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v10.15.3/bin/npm
  Browsers:
    Chrome: 78.0.3904.97
    Safari: 13.0.3
  npmPackages:
    @storybook/react: ^5.2.6 => 5.2.6 
@tonyjmartinez
Copy link
Author

Found my problem... I wasn't exporting a proper React component.
This export const card = <Card>This is a card</Card>;
Should have been this export const card = () => <Card>This is a card</Card>;

@mnpenner
Copy link

Could we make this error message a little better? I just hit this one too and it was super confusing.

Also a warning about missing a default export in a .stories.tsx file would nice for us newcomers.

@shilman
Copy link
Member

shilman commented Jan 15, 2020

Good idea @mnpenner - any interest in trying to fix this?

@sunnykgupta
Copy link

I just faced this. Clearly it is caused by not reading the docs better 😬
Glad I was able to find my way to this issue and quickly identify my mistake.

@XingXiaoWu
Copy link

same question.
but it looks like can't use Upper Camel Case
just like

export const Basic = () => ({...})

image

when I use little camel-case

export const basic = () => ({..})

it's ok.
image

@shilman
Copy link
Member

shilman commented Jun 8, 2020

@XingXiaoWu do you have a repro? we recommend TitleCase for story names and use that all the time

@XingXiaoWu
Copy link

@XingXiaoWu您是否有代表?我们建议使用TitleCase作为故事名称,并一直使用

I can provide it,but Vue
vue-demo.zip

Startup Steps:

npm i
npm run storybook

File location

The story file is located in vue-demo/stories/ 3-freetable.stories.js
The Component file is located in vue-demo/src/component

Repetition steps

//in vue-demo/stories/ 3-freetable.stories.js
export const basic = () => ({..})

Modified to

//in vue-demo/stories/ 3-freetable.stories.js
export const Basic = () => ({..})

Expected behavior

Normal behavior

Actual behavior

Error : fn.apply is not a function

@towry
Copy link

towry commented Jun 22, 2020

@ XingXiaoWu do you have a repro? we recommend TitleCase for story names and use that all the time

I have the same issue, my example is const Example = ...., I have to change Example to example.

deps:

    "@storybook/addon-actions": "^5.3.19",
    "@storybook/addon-knobs": "^5.3.19",
    "@storybook/addon-notes": "^5.3.19",
    "@storybook/addons": "^5.3.19",
    "@storybook/preset-scss": "^1.0.2",
    "@storybook/preset-typescript": "^3.0.0",
    "@storybook/vue": "^5.3.19",

@shilman
Copy link
Member

shilman commented Jun 22, 2020

@towry do you have a public repro? i can't imagine why this problem might occur.

@towry
Copy link

towry commented Jun 22, 2020

@towry do you have a public repro? i can't imagine why this problem might occur.

I just created one: https://github.com/pze/storybook-vue-issue

npm run storybook

@alwkins
Copy link

alwkins commented Jul 10, 2021

Thanks everyone, for the tip to check for missing () =>
I saw the same fn.apply error when trying to pass in storyFn.

Defined storyFn:

export const MyComponentStory = () => {
    return (
        <MyComponent />
    );
}

This usage caused the error:

storiesOf('Components', module).add('MyComponent', MyComponentStory());

It resolved when I added the () =>:

storiesOf('Components', module).add('MyComponent', () => MyComponentStory());

@akbarkz
Copy link

akbarkz commented Dec 20, 2021

I have a similar issue when using Vite as a bundler. I posted my question with all the details on SO here: https://stackoverflow.com/questions/70420749/storybook-with-vite-error-fn-apply-is-not-a-function

@IanVS
Copy link
Member

IanVS commented Dec 28, 2022

The stack overflow solution is that a decorator was causing the problem, so it sounds like folks here might want to check that any decorators they are using (including global decorators in preview.js) are correct.

I'd also recommend that everyone try out CSF3, in which stories are exported as objects, rather than functions.

@shilman shilman removed the todo label Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants