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?
Since Gatsby natively supports JavaScript and TypeScript, you can change files from . js / . jsx to . ts / .
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With