LoginSignup
0
0

More than 1 year has passed since last update.

react-intl-universalをStorybookに反映させる

Last updated at Posted at 2022-09-22

ちょっと引っかかったのでメモ。Storybookに用意されている decorators を使えば解決する話でした。

/* .storybook/preview.js */
import React from 'react';
import intl from 'react-intl-universal';
import jaJP from './assets/locales/ja-JP.json';

const currentLocale = 'ja-JP';
const localeData = jaJP;

const withIntl = (Story) => {
  React.useEffect(() => {
    intl.init({
      currentLocale,
      locales: { [currentLocale]: localeData },
      commonLocaleDataUrls,
    });
  }, []);
  return <Story />;
};

export const decorators = [withIntl];

export するものの名前は decorators にしておく。

decoratorsの詳細は以下。
https://storybook.js.org/docs/react/writing-stories/decorators

なお、言語情報のファイルはWebpack内でcopy-webpack-pluginを使ってコピーしています。

/* .storybook/main.js */
const CopyWebpackPlugin = require('copy-webpack-plugin');
const appDir = '../src';

module.exports = {
  /* 略 */
  core: {
    builder: 'webpack5',
  },

  webpackFinal: async (initConfig) => {
    const config = initConfig;

    // 静的なファイルをコピー
    config.plugins.push(new CopyWebpackPlugin({
      patterns: [{
        from: `${appDir}/assets`,
        to: `${__dirname}/assets`,
      }],
    }));
    /* 略 */
    return config;
  },
};

おわり。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0