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.

useAddonState: cannot read property 'getAddonState' of undefined

See original GitHub issue

Describe the bug I’m trying to create a custom addon to allow theme switching from an addon panel using Material UI. This means using state shared between the panel and ThemeProvider wrapped story.

When using the useAddonState hook from ‘@storybook/api’, useAddonState inside an index.js file for a custom addon produces the error Cannot read property ‘getAddonState’ of undefined. The hook works fine in the register.js file

To Reproduce Steps to reproduce the behavior:

  1. Follow the steps in Writing Addons up-to and including “Using the complex addon”
  2. run yarn add @storybook/api -D
  3. Add the line import { useAddonState } from '@storybook/api' to the index.js file of the custom addon
  4. Add the line const [state, setState] = useAddonState('my/addon', 'test string') into the wrapper function
  5. Run storybook, inside the story area there should be the error “Cannot read property ‘getAddonState’ of undefined”

Expected behavior Nothing serious will happen because the state is not being used yet but storybook should not display any errors.

Screenshots Screenshot 2019-10-22 at 15 50 43

Code snippets register.js

import React from 'react'
import addons from '@storybook/addons'
import { useChannel } from '@storybook/api'
import { AddonPanel } from '@storybook/components'
import { STORY_RENDERED } from '@storybook/core-events'

const MyPanel = () => {
  const emit = useChannel({
    STORY_RENDERED: id => {
      console.log('render')
    },
    'my/customEvent': pars => {
      console.log('click', pars)
    }
  })

  return <button onClick={() => emit('my/otherEvent')}>click to emit</button>
}

// Register the addon with a unique name.
addons.register('my/addon', api => {
  // Also need to set a unique name to the panel.
  addons.addPanel('my/addon/panel', {
    title: 'My Addon',
    render: ({ active, key }) => (
      <AddonPanel key={key} active={active}>
        <MyPanel />
      </AddonPanel>
    )
  })
})

index.js

import React from 'react'
import addons, { makeDecorator } from '@storybook/addons'
import { useAddonState } from '@storybook/api'
import BrandProvider from '../../src/components/BrandProvider/BrandProvider'

export default makeDecorator({
  name: 'withMyAddon',
  parameterName: 'myParameter',
  skipIfNoParametersOrOptions: false,
  wrapper: (getStory, context, { parameters }) => {
    const [state, setState] = useAddonState('my/addon', 'test string')
    return <BrandProvider brand={'test'}>{getStory(context)}</BrandProvider>
  }
})

System: Environment Info: System: OS: macOS Mojave 10.14.6 CPU: (12) x64 Intel® Core™ i7-8850H CPU @ 2.60GHz Binaries: Node: 11.13.0 - /usr/local/bin/node Yarn: 1.19.1 - /usr/local/bin/yarn npm: 6.11.3 - /usr/local/bin/npm Browsers: Chrome: 77.0.3865.120 Safari: 13.0.2 npmPackages: @storybook/addons: ^5.2.5 => 5.2.5 @storybook/api: ^5.2.5 => 5.2.5 @storybook/components: ^5.2.5 => 5.2.5 @storybook/core-events: ^5.2.5 => 5.2.5 @storybook/react: ^5.2.5 => 5.2.5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
ndelangencommented, Mar 19, 2020
0reactions
kwhitejrcommented, Mar 10, 2020

@Reignable can you share how you implemented state-sharing with useChannel?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Storybook: Addon-Docs Cannot read property 'classes' of ...
Having added, and configured, @storybook/addon-docs the Storybook Docs page displays: “Cannot read property 'classes' of undefined” in the ...
Read more >
Addon API - Storybook - JS.ORG
The render function is called with active and key . The active value will be true when the panel is focused on the...
Read more >
storybook cannot read properties of undefined - You.com
The TypeError: Cannot read properties of undefined (reading 'type') exception occurs in storybook when one defines the argTypes in the default export object ......
Read more >
Renovate Bot Package Diff
getAddonState (addonId)):nextState=newStateOrMerger;return store. ... instanceof Constructor))throw new TypeError("Cannot call a class as a function")}(this ...
Read more >
How to use the @storybook/api.useAddonState function in ...
export const CoveragePanel: React.FC<{}> = () => { const [coverageSummary, setCoverageSummary] = useAddonState<CoverageSummary>(ADDON_ID, null); ...
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