Open schalkneethling opened 4 years ago
Update: Based on the deprecation warning in the devtools I changed all instances of Preview
to Canvas
inside the mdx story file and restarted Storybook.
Preview doc block has been renamed to Canvas.
https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#previewprops-renamed
Storybook builds and now shows all the button instances that do not use an SVG component(when viewed inside the Canvas tab).
When I click on the Story in the sidebar for the button that does use an SVG component, the error in the Storybook UI is as follows:
Cannot read property 'displayName' of undefined
TypeError: Cannot read property 'displayName' of undefined
at getReactElementDisplayName (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:157192:23)
at parseReactElement (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:157227:21)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:157243:12
at Array.map (<anonymous>)
at parseReactElement (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:157242:106)
at reactElementToJsxString (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:157706:21)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:7949:76
at mapSingleChildIntoContext (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:163754:26)
at traverseAllChildrenImpl (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:163612:5)
at traverseAllChildren (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:163697:10)
at mapIntoWithKeyPrefixInternal (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:163779:3)
at Object.mapChildren [as map] (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:163803:3)
at renderJsx (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:7946:43)
at jsxDecorator (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:8000:18)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:24402:21
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:26153:14
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:26160:16
at wrapper (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:24141:12)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:24920:14
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:24934:26
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:24402:21
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:26153:14
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:26160:16
at wrapper (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:3668:12)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:24920:14
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:24934:26
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:24402:21
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:26153:14
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:24430:20
at storyFn (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:27554:30)
at renderWithHooks (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:146587:18)
at mountIndeterminateComponent (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:149266:13)
at beginWork (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:150380:16)
at HTMLUnknownElement.callCallback (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:131972:14)
at Object.invokeGuardedCallbackDev (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:132021:16)
at invokeGuardedCallback (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:132076:31)
at beginWork$1 (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:154987:7)
at performUnitOfWork (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:153941:12)
at workLoopSync (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:153914:22)
at performSyncWorkOnRoot (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:153540:9)
at scheduleUpdateOnFiber (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:152972:7)
at updateContainer (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:156157:3)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:156542:7
at unbatchedUpdates (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:153687:12)
at legacyRenderSubtreeIntoContainer (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:156541:5)
at Object.render (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:156624:10)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:187731:26
at new Promise (<anonymous>)
at render (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:187730:10)
at _callee$ (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:187820:20)
at tryCatch (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:179215:40)
at Generator.invoke [as _invoke] (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:179444:22)
at Generator.prototype.<computed> [as next] (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:179267:21)
at asyncGeneratorStep (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:187701:103)
at _next (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:187703:194)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:187703:364
at new Promise (<anonymous>)
at StoryRenderer.<anonymous> (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:187703:97)
at StoryRenderer.renderMain [as render] (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:187793:22)
at _callee$ (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:186610:33)
at tryCatch (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:179215:40)
at Generator.invoke [as _invoke] (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:179444:22)
at Generator.prototype.<computed> [as next] (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:179267:21)
at asyncGeneratorStep (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:186280:103)
at _next (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:186282:194)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:186282:364
at new Promise (<anonymous>)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:186282:97
at StoryRenderer.renderStory (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:186630:12)
at StoryRenderer.renderStoryIfChanged (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:186520:18)
at StoryRenderer.renderCurrentStory (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:186431:12)
at Object.<anonymous> (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:186359:24)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:25723:14
at Array.forEach (<anonymous>)
at Channel.handleEvent (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:25722:19)
at handler (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:25648:16)
at Channel.emit (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:25655:9)
at StoryStore.setSelection (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:27747:23)
at Object.<anonymous> (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:27269:23)
at http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:25723:14
at Array.forEach (<anonymous>)
at Channel.handleEvent (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:25722:19)
at PostmsgTransport.<anonymous> (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:25612:22)
at PostmsgTransport.handler (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:25270:17)
at PostmsgTransport.handleEvent (http://localhost:6006/vendors~main.ae048bfc7abe27cf76c3.bundle.js:25444:16)
Seems that witing the context of the Story
the SVG component is undefined
aka out of scope? I also tried not nesting the Story
block inside anything, but that produces the same error as the original issue detailed.
Clicking on the "Docs" tab renders a blank docs page irrespective of the button selected in the sidebar. Is this the expected behavior?
Update 2: I thought perhaps from within an arrow function call might be causing the problem and so tried:
<Canvas>
<Story name="Icon button">
{
<Button state="positive icon right">
Icon on right <Github />
</Button>
}
</Story>
</Canvas>
The same end result though unfortunately.
Apologies for all the comments folks, hopefully it is more useful than distracting.
Update 3: With the following:
## Icon buttons
When using an icon within a button you have the option of showing the icon either on the left or right of the label text.
#### Icon on the right
<Canvas>
<Story
name="Icon right button"
args={{
state: "positive icon right",
}}
>
{(args) => (
<Button {...args}>
Icon on right <Arrow />
</Button>
)}
</Story>
</Canvas>
#### Icon on the left
<Canvas>
<Story
name="Icon left button"
args={{
state: "positive icon left",
}}
>
{(args) => (
<Button {...args}>
<Github /> Icon on right
</Button>
)}
</Story>
</Canvas>
I now get a slightly different error in devtools:
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `storyFn`.
in storyFn
in ErrorBoundary
and
Uncaught TypeError: element.type is undefined
getReactElementDisplayName index.js:339
parseReactElement index.js:374
childrens index.js:390
parseReactElement index.js:389
reactElementToJsxString index.js:853
result jsxDecorator.js:101
React 5
renderJsx jsxDecorator.js:98
jsxDecorator jsxDecorator.js:152
hookify hooks.js:247
defaultDecorateStory decorators.js:36
defaultDecorateStory decorators.js:43
wrapper preview.js:217
decorator make-decorator.js:31
makeDecorator make-decorator.js:45
hookify hooks.js:247
defaultDecorateStory decorators.js:36
defaultDecorateStory decorators.js:43
wrapper withActions.js:189
decorator make-decorator.js:31
makeDecorator make-decorator.js:45
hookify hooks.js:247
defaultDecorateStory decorators.js:36
applyHooks hooks.js:275
storyFn story_store.js:620
React 16
render render.js:84
render render.js:83
_callee$ render.js:173
Babel 8
renderMain render.js:146
_callee$ StoryRenderer.js:370
Babel 8
renderStoryIfChanged StoryRenderer.js:280
renderCurrentStory StoryRenderer.js:191
setupListeners StoryRenderer.js:119
handleEvent index.js:196
handleEvent index.js:195
handler index.js:121
emit index.js:128
setSelection story_store.js:813
finishConfiguring story_store.js:436
configure config_api.js:33
loadCsf loadCsf.js:326
configure index.js:35
js generated-stories-entry.js:5
js main.b72c3840c569fb8f4544.bundle.js:116
Webpack 7
do you have the appropriate loaders set up to load your SVGs as React elements?
do you have the appropriate loaders set up to load your SVGs as React elements?
The app is based on CRA which does have the relevant loaders. I assumed(perhaps incorrectly) that Storybook is simply reusing these in the build pipeline?
aha, didn't realize that was built into CRA. do you have the CRA preset installed?
aha, didn't realize that was built into CRA. do you have the CRA preset installed?
Yup - https://github.com/mdn/yari/blob/mdn-fiori/package.json#L78
Here is the CRA info - https://create-react-app.dev/docs/adding-images-fonts-and-files/#adding-svgs
cc @mrmckeb
Hi @schalkneethling, this is working in our tests - you can see here that Button
contains an SVG - but it may be something related to your environment. I haven't checked out the project locally, but I had a quick look at this:
https://raw.githubusercontent.com/mdn/yari/mdn-fiori/client/src/ui/docs/atoms/button.stories.mdx
I can't actually see where "../../dinocons/arrows/arrow.svg"
is supposed to come from. It isn't in this directory: https://github.com/mdn/yari/tree/mdn-fiori/client/src/ui/docs/dinocons
Perhaps there's something not committed to the project? Or perhaps I need to run it locally and build something first?
@mrmckeb Thank you for taking a look. The source might have changed since I opened the PR but, let me double-check this locally.
Hey @mrmckeb @shilman, I am still experiencing this in Storybook v6.0.6 unfortunately. Inside an mdx
file I have for example:
import { ReactComponent as Arrow } from "@mdn/dinocons/arrows/arrow.svg";
<IconGallery>
<IconItem name="arrow">
<Arrow />
</IconItem>
</IconGallery>
The above resolves just fine i.e. no error of a reference error when starting up Storybook The same error is thrown in devtools though.
When I do the same inside a stories.js
file, everything works fine. However, if I then try to import and use IconGallery
or IconItem
inside this file, I get the error as reported here: https://github.com/storybookjs/storybook/issues/12016
I have pushed the latest to code to the repo. You can see the two files here: https://github.com/mdn/yari/tree/1e120a3317a4203c01dcaf58ed926f6efee9f47f/client/src/ui/docs/dinocons
The mdx example fails as explained but the js
works as expected, unless I try to use either IconGallery
or IconItem
In the following commit I found a workaround: https://github.com/mdn/yari/tree/54037b63ab1cd00ac1e77fc0bee15a988a16b98c/client/src/ui/docs/dinocons
I'm having the same issue. Just upgraded to v6 and every story that imports an SVG file has a scary Element type is invalid
error. Also using CRA.
@benknight are you also using MDX?
@shilman I have not. Admittedly I haven't dug very deep into this. Basically I upgraded to v6, removed the CRA add-on, and ran storybook, then saw the errors only on components that import svgs as ReactComponent
Ok, was only asking because it looks like it's working for @schalkneethling in JS files and only broken in MDX (which is a little weird)
@shilman my bad, I misread the upgrade guide and thought I had to remove @storybook/preset-create-react-app
. No more issues.
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!
I am facing same issue. Using mdx file for CRA app.
@jimishshah do you have the CRA preset installed?
Update: I have switched to using .js
and CSF so I am no longer "blocked" by this but, last I tried, it still failed for me(with @storybook/preset-create-react-app
).
I'm facing the same problem with the same setup (Storybook 6, CRA 3.4.3). My fix was to create an index file for our icons:
// /assests/icons/index.ts
export { ReactComponent as IconShare } from "./iconShare.svg";
export { ReactComponent as IconPotato } from "./IconPotato.svg";
// ...
and then use the re-exported version on my stories:
import { IconShare, IconPotato } from 'assets/images';
<Canvas>
<Story name="icon" >
{(args) => <Button {...args} icon={<IconPotato />}/>}
</Story>
</Canvas>
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!
I'm not sure if I had exactly the same issue but I modified the webpack config like in the doc and I was able to display any svg using @svgr/webpack:
Icon import:
import { ReactComponent as IconShare } from "./iconShare.svg";
.storybook/main.js
module.exports = {
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
webpackFinal: async (config, { configType }) => {
const assetRule = config.module.rules.find(({ test }) => test.test('.svg'));
const assetLoader = {
loader: assetRule.loader,
options: assetRule.options || assetRule.query,
};
config.module.rules.unshift({ test: /\.svg$/, use: ['@svgr/webpack', assetLoader] });
// Return the altered config
return config;
},
};
I'm not sure if I had exactly the same issue but I modified the webpack config like in the doc and I was able to display any svg using @svgr/webpack:
Icon import:
import { ReactComponent as IconShare } from "./iconShare.svg";
.storybook/main.js
module.exports = { stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'], addons: ['@storybook/addon-links', '@storybook/addon-essentials'], webpackFinal: async (config, { configType }) => { const assetRule = config.module.rules.find(({ test }) => test.test('.svg')); const assetLoader = { loader: assetRule.loader, options: assetRule.options || assetRule.query, }; config.module.rules.unshift({ test: /\.svg$/, use: ['@svgr/webpack', assetLoader] }); // Return the altered config return config; }, };
I have been trying this solution but I get this error, any ideas @darkship ?
Cannot read property 'test' of undefined
@geocine Are you using Storybook with Create React App? This is built-in to the CRA preset.
@geocine you also can do
webpackFinal: async (config, { configType }) => {
config.module.rules.unshift({ test: /\.svg$/, use: ['@svgr/webpack', {
//custom options, like you do in the webpack config
}] });
// Return the altered config
return config;
},
The trick with the match
was only to reuse the existing configuration from storybook. It's not necessary and could not work in this case.
@geocine you also can do
webpackFinal: async (config, { configType }) => { config.module.rules.unshift({ test: /\.svg$/, use: ['@svgr/webpack', { //custom options, like you do in the webpack config }] }); // Return the altered config return config; },
The trick with the
match
was only to reuse the existing configuration from storybook. It's not necessary and could not work in this case.
This also broke my build, along with all the other workarounds except creating a separate file for importing svgs as @PupoSDC suggested, same story (Storybook 6.1, CRA 4, using mdx
).
Also encountered with this issue, failed at starting storybook
config.module.rules.push({
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
include: /src\/components\/Menu/,
use: [
{
loader: '@svgr/webpack',
options: {
babel: false,
icon: true,
},
},
],
});
ERROR in ./src/components/Menu/dashboard.source.svg 3:33 Module parse failed: Unexpected token (3:33) File was processed with these loaders:
./node_modules/@svgr/webpack/lib/index.js You may need an additional loader to handle the result of these loaders. | import * as React from "react"; |
---|
function SvgDashboardsource(props: React.SVGProps
) { | return <svg width="1em" height="1em" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" {...props}> ; | } @ ./src/components/Menu/index.tsx 53:0-50 422:21-33
I found a solution and it works for me ,
//storybook main.js
module.exports = {
.....
webpackFinal: async (config, { configType }) => {
const fileLoaderRule = config.module.rules.find(
(rule) => rule.test && rule.test.test('.svg')
);
fileLoaderRule.exclude = /\.svg$/;
config.module.rules.push({
test: /\.svg$/i,
enforce: 'pre',
issuer: /\.[jt]sx?$/,
use: [{ loader: '@svgr/webpack', options: { icon: true } }],
include: path.resolve(__dirname, '../src'),
});
return config;
}};
//global.d.ts file
declare module '*.svg' {
const content: any;
export default content;
}
It solved my problem so here is the reference link https://stackoverflow.com/a/61706308
I have the same issues, error:Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Describe the bug
In a
mdx
story file I have the following:Further down in the same file I then have:
Starting up Storybook happens without any problems but upon launching it in the browser emits the following error in Developer Tools:
It seemed to me that my code was sound and so I tested this inside a
tsx
file inside the app i.e. outside of Storybook:Starting up the app and loading it in the browsers renders the button with the SVG without any errors so, this seems to be a bug in how Storybook handles the above case when embedded inside a
Story
Expected behavior
It renders the button with the relevant text and SVG element
System: