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.

vue3 jsx produces "React is not defined"

See original GitHub issue

i am trying to implement storybook with antdesignvue i get the error “ReferenceError: React is not defined” when i tried to import a tsx file which returns jsx in button.stories.js file (screenshot is attached) image

my settings are

//vite.config.ts

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import Components from "unplugin-vue-components/vite";
import vueJsx from "@vitejs/plugin-vue-jsx";

// https://vitejs.dev/config/
export default defineConfig({
    esbuild: {
    jsxFactory: 'h',
    jsxFragment: 'Fragment'
    },
    plugins: [
    vue(),
    vueJsx(),
    Components({
        dirs: ["./components"],
    }),
    ],
});

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": "./",
    "paths": {
      "ant-design-vue": ["components/index.ts"],
      "ant-design-vue/es/*": ["components/*"],
      "@components/*": ["./components/*"],
      "@assets/*": ["./assets/*"],
    },
    "lib": [
      "esnext",
      "dom"
    ],
    "strictNullChecks": false,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "jsx": "preserve",
    "noUnusedParameters": true,
    "noUnusedLocals": true,
    "noImplicitAny": false,
    "skipLibCheck": true,
    "importsNotUsedAsValues": "preserve",
    "target": "es5",
    "useDefineForClassFields": true,
    "module": "esnext",
    "strict": false,
    "sourceMap": true,
    "resolveJsonModule": true,
  },
  "exclude": ["node_modules", "lib", "es", "dist", "v2-doc", "scripts", "**/__tests__/**/*"],
  "forceConsistentCasingInFileNames": true,
  "isolatedModules": true,
  "lib": ["dom", "dom.iterable", "esnext"],
  "module": "esnext",
  "moduleResolution": "node",
  "noEmit": true,
  "noFallthroughCasesInSwitch": true,
  "resolveJsonModule": true,
  "skipLibCheck": true,
  "strict": true,
  "target": "es5",
  "types": ["node", "vite/client"],
  "include": [
    "components/**/*.ts",
    "components/**/*.d.ts",
    "components/**/*.tsx",
    "components/**/*.vue"
  ]
}

Button.stories.js


import Button from "../components/button/button";

export default {
  title: "Button",
  components: { Button },
};

const Template = (args) => ({
  props: Object.keys(args),
  components: { Button },
  setup() {
    return { args };
  },
  template: `
    <Button v-bind="args" />
  `,
});

export const Hello = Template.bind({});
Hello.args = {};

error screenshot is image

every kind of help is much appreciated

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
maicolbruskicommented, Jun 2, 2022

You are Genius!

Works perfect with:

.storybook/main.js

module.exports = {
  stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: ["@storybook/addon-links", "@storybook/addon-essentials", "@storybook/addon-interactions"],
  framework: "@storybook/vue3",
  core: {
    builder: "@storybook/builder-vite",
  },
  features: {
    storyStoreV7: true,
  },
  async viteFinal(config) {
    config.plugins = [
      ...config.plugins,
      require("@vitejs/plugin-vue-jsx")({
        exclude: [/\.stories\.(t|j)sx?$/, /node_modules/],
      }),
    ];
    return config;
  },
};

Tks @IanVS !!!

1reaction
IanVScommented, Jun 2, 2022

It looks like you are missing https://github.com/vitejs/vite/tree/master/packages/plugin-vue-jsx from the vite config. Adding vite plugins is a little bit tricky right now, but it can be done in viteFinal. Here’s an example of someone changing the plugins: https://github.com/storybookjs/builder-vite/issues/286#issue-1176508222. I think all you’ll need to do is push the one you want onto the end of the existing config.plugins.

Read more comments on GitHub >

github_iconTop Results From Across the Web

`ReferenceError: React is not defined` in vue3 jsx files
stories. js, I got "ReferenceError: React is not defined" you can see error here but react is not the part of this project...
Read more >
Render Functions & JSX | Vue.js
Although first introduced by React, JSX actually has no defined runtime semantics and can be compiled into various different outputs.
Read more >
Using JSX with Vue - LogRocket Blog
This post will look at render functions and how they work along with JSX and why you might want to use it in...
Read more >
Introducing JSX - React
JSX produces React “elements”. We will explore rendering them to the DOM in the next section. Below, you can find the basics of...
Read more >
Features | Vite
The pre-bundling step is performed with esbuild and makes Vite's cold start ... If not using JSX with React or Vue, custom jsxFactory...
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