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.

Redundant React Imports in next.js/examples

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

Currently most examples in the examples folder include react imports in files under pages which is not needed. I can’t recall if it ever was needed in Next.js (Perhaps some examples with an older next.js version need the imports)

Describe the solution you’d like

Remove react imports from files that don’t need. Method of testing is up to you to decide but I presume running the project, checking for console errors and making sure the functionality is the same should be sufficient?

Alternatives Considering whether the changes are worth it

Is this something that the Next.js team think should be changed? It’s perfectly reasonable if you guys think this isn’t worth the effort. With that said, I would love the honor of contributing to the Next.js repo, even with something that is probably considered a chore.

I don’t know if there are any technical differences with redundantly importing React but it could be argued that’s it’s bad practice.

Additional context

Some components are written with classes where as far as I can tell React imports are needed.

import React, { PureComponent } from 'react'
import { connect } from 'react-redux'
import Link from 'next/link'
import { startClock, serverRenderClock } from '../actions'
import Examples from '../components/examples'

class Index extends PureComponent {
//...
}

Should those be updated to functional components or be left alone?

I also forked the repo just to showcase what the differences would be using the with-redux and with-redux-thunk examples. with-redux-thunk uses a class component in pages/index.js so there I didn’t remove the React import.

Git diff

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

6reactions
timneutkenscommented, May 16, 2020

Next.js automatically adds the React import when JSX is used indeed. However keep in mind that we do still need to import React from 'react' when the React variable is used. Besides that yeah it seems like a good one to contribute 👍 Marked as good first issue.

2reactions
Timercommented, May 27, 2020

Exciting work! Thanks to all involved. FWIW, we get all notifications, so it’s no biggy!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature request - GitHub
Next.js automatically adds the React import when JSX is used indeed. However keep in mind that we do still need to import React...
Read more >
Advanced Features: Dynamic Import - Next.js
Dynamically import JavaScript modules and React Components and split your code into manageable chunks.
Read more >
Basic Features: Handling Scripts - Next.js
Optimize your third-party scripts with the built-in `next/script` component.
Read more >
What is Code Splitting? - How Next.js Works
Dynamic imports are another way to manually split what code is initially loaded. Quick Review: Which of the following is not a goal...
Read more >
Advanced Features: Output File Tracing - Next.js
Next.js automatically traces which files are needed by each page to allow for easy deployment of your application. Learn how it works here....
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