Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding gatsby-plugin-typescript to GatsbyJS "React is not defined"

I created the hello world GatsbyJS application and then added the typescript plugin and I immediately got the error "React is not defined". I went through the steps of adding "import * as React from 'react'"; but I still have the same error being thrown in /cache/app.js. I am not sure about the next steps.

Hello world GatsybyJS: https://www.gatsbyjs.org/tutorial/part-zero/#create-a-gatsby-site

Adding Typescript: https://www.gatsbyjs.org/packages/gatsby-plugin-typescript/?=by-config.js

Has anyone had this same issue?

like image 694
Josh Bowden Avatar asked Jan 23 '19 16:01

Josh Bowden


People also ask

Can you use TypeScript with Gatsby?

Since Gatsby natively supports JavaScript and TypeScript, you can change files from . js / . jsx to . ts / .

How Gatsby works with React?

Using React with GatsbyGatsby enables frontend developers to iterate quickly on React websites, by solving common problems like: Pulling in data. In Gatsby, GraphQL and plugins help you use data from nearly any source (including both traditional CMSs and headless CMSs). Creating pages and routes.

What is Gatsby plugin React helmet?

Provides drop-in support for server rendering data added with React Helmet. React Helmet is a component which lets you control your document head using their React component.


2 Answers

In my case, I ran into this problem when I configured the typescript like this:

  plugins: [
    {
      resolve: `gatsby-plugin-typescript`,
      options: {
        isTSX: true, // defaults to false
        jsxPragma: `jsx`, // defaults to "React"
        allExtensions: true, // defaults to false
      },
    },
    // (continues...)
  ]

I changed into the default plugin:

  plugins: [
    `gatsby-plugin-typescript`,
    // (continues...)
  ]

This fixed the problem for me.

I found this fix from following article: https://github.com/ChristopherBiscardi/gatsby-mdx/issues/358

like image 137
Yuki Inoue Avatar answered Oct 05 '22 18:10

Yuki Inoue


This configuration in the page on adding Typescript to Gatsby

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-typescript`,
        options: {
          isTSX: true, // defaults to false
          jsxPragma: `jsx`, // defaults to "React"
          allExtensions: true, // defaults to false
        },
     },
  ],
}

If you change this line

jsxPragma: `jsx`,

to

jsxPragma: `React`,

your code will work.

like image 23
gellati Avatar answered Oct 05 '22 18:10

gellati