Skip to content
This repository was archived by the owner on Jan 31, 2024. It is now read-only.
This repository was archived by the owner on Jan 31, 2024. It is now read-only.

"Storybook preview hooks can only be called inside decorators and story functions." error #30

@neil-morrison44

Description

@neil-morrison44

Describe the bug

We've got a custom addon that we've written internally to allow us to document / manipulate our i18n translations inside storybooks & we're having trouble getting it to work with this.

So far I've:

  • Started to set up @storybookjs/testing-react (which, once we get it working will save a lot of time & be incredibly useful so thanks)
  • Noticed that the decorator provided by the addon wasn't being applied (which I guess makes sense? the .storybook/main.js with the addon array isn't referenced anywhere)
  • Thought I'd use the setGlobalConfig to manually add in the decorator from our addon
  • Got the Storybook preview hooks can only be called inside decorators and story functions. error

Our decorator looks something like:

import React, { useEffect, useState } from "react"
import { I18nContext } from "@our_stuff/i18nContext"
import I18n from "@our_stuff/i18n"
import { makeDecorator, useGlobals, useParameter } from "@storybook/addons"

export const withOurI18n = makeDecorator({
  name: "withOurI18n",
  parameterName: "i18n",
  wrapper: (storyFn, context, { parameters }) => {
    const paramI18nData = useParameter("i18n", {})
    const [{ "our-intl": intlData }] = useGlobals()
    const [i18n, setI18n] = useState(getNewI18nForData(intlData || paramI18nData))

    useEffect(() => {
      if (intlData) setI18n(I18n(intlData))
    }, [intlData])

    return <I18nContext.Provider value={i18n}>{storyFn()}</I18nContext.Provider>
  },
})

(it's rough & has bugs but it works when viewing one story in storybook)

Our jest.setup.js looks like:

import { setGlobalConfig } from "@storybook/testing-react"
import * as globalStorybookConfig from "../.storybook/preview"
import { withOurI18n } from "@our_stuff/storybook-addon-i18n/dist/decorators/withI18n"

setGlobalConfig({ ...globalStorybookConfig, decorators: [withOurI18n] })

At the moment our test looks pretty much exactly like the one in the getting started documentation

Let me know if there's anything else I can provide that'd be useful

Expected behavior
Would expect the decorators to function the same way they do when viewing the story in the storybook.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions