Skip to content

Expecting a react element from stories for global IntlProvider decorator #1240

@sthuber90

Description

@sthuber90

Hello,

I would like to use storybook for a component of mine which uses react-intl. Preferably I would use the storybook-addon-intl for that matter, which unfortunately doesn't seem to support the current version of storybook yet. So to be able to use storybook nonetheless I edited .storybook/config.js to:

import React from 'react';
import { configure, setAddon, addDecorator } from '@storybook/react';
import infoAddon from '@storybook/addon-info';
import { IntlProvider } from 'react-intl';

// add info addon
setAddon(infoAddon);

// add a decorator to wrap stories rendering into IntlProvider
const DEFAULT_LOCALE = 'en';
const DEFAULT_MESSAGES = require('../src/translations/locales/en')

addDecorator(story => {
  <IntlProvider locale={DEFAULT_LOCALE} key={DEFAULT_LOCALE} messages={DEFAULT_MESSAGES}>
     { story() }
  </IntlProvider>
})

//stories loader
const req = require.context('../src', true, /\.stories\.js$/)
function loadStories() {
  req.keys().forEach((filename) => req(filename))
}

// Run storybook
configure(loadStories, module);

The component I try to test / start with is Loading.storybook.js:

import React from 'react';
import { storiesOf } from '@storybook/react';
import Loading from '../components/Loading';

import '../styles/compiled-css/style.css';
import '../index.css';

//add a decorator to wrap stories rendering into IntlProvider
const DEFAULT_LOCALE = 'en';
const DEFAULT_MESSAGES = require('../translations/locales/en')

storiesOf('Loading', module)
  .add('default', () => (
     <Loading />
  ));

Unfortunately when running storybook I keep getting the error Expecting a react element for the story: "default" from "Loading".

Any idea why this is happening? I also reinstalled node_modules and checked that I don't use any storybook addon with old dependencies.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions