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.

Next.js should not automatically expose the `React` global

See original GitHub issue

Bug report

Describe the bug

When you use JSX in your app, we automatically expose the React global. This allows things like React.useState to work even though it’s not imported.

To Reproduce

Use React.useState in your app without an import, and see it work.

Expected behavior

It should not work, and instead, require React be imported.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
timneutkenscommented, Jul 13, 2020

would adding the ‘babel-plugin-react-require’ plugin solve this issue?

It would not

Would this mean that pages now should import React as well? What is the rational behind this? I am just curious.

Only when React is used it should be imported. Using JSX is fine without importing:

export default function Page() { return <h1>Hello</h1> }

When doing a useState however it’s non-standard that this currently works:

export default function Page() {
  // This should not be possible as it needs React to function
  const [state, setState] = React.useState(false) 
  return <button onClick={() => setState(true)}>{state ? 'State set' : 'set the state'}</button>
}

It should be written as:

import React from 'react'

export default function Page() {
  const [state, setState] = React.useState(false) 
  return <button onClick={() => setState(true)}>{state ? 'State set' : 'set the state'}</button>
}
1reaction
Timercommented, Nov 13, 2020

Considering this fixed when you upgrade to React 17.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating from Create React App - Next.js
Migrating from Create React App. This guide will help you understand how to transition from an existing non-ejected Create React App project to...
Read more >
Basic Features: Environment Variables - Next.js
By default environment variables are only available in the Node.js environment, meaning they won't be exposed to the browser. ... This loads process.env....
Read more >
next.config.js: Environment Variables
Learn to add and access environment variables in your Next.js application at build time.
Read more >
next.config.js: Runtime Configuration
To add runtime configuration to your app, open next.config.js and add the publicRuntimeConfig and serverRuntimeConfig configs:
Read more >
CDN Support with Asset Prefix - next.config.js
Attention: Deploying to Vercel automatically configures a global CDN for your ... Do not upload the rest of your .next/ folder, as you...
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