Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invariant failed: Browser history needs a DOM

My App.js (simplified but still gives error with just this much.)

import React, { Component } from "react";

class App extends Component {
  state = {};

  componentDidMount() {
    this.setState({ user });
  }
}

export default App;

I have updated my index.js now:

import React from "react";
import { BrowserRouter } from "react-router-dom";
import App from "./App";

const Index = () => (
  <BrowserRouter>
    <App />
  </BrowserRouter>
);

export default Index;

It gives the error Invariant failed: Browser history needs a DOM

like image 810
Keytoll Avatar asked Oct 20 '25 12:10

Keytoll


1 Answers

In NextJS, you don't have to add the ReactDOM.render part as in normal ReactJS applications. NextJS does this themselves while rendering the application.

The bare minimum, you have to have the appication running is to add a index.js in pages directory:

const Index = () => (
  <div>
    <p>Hello Next.js</p>
  </div>
);

export default Index;

Note that Index component here is exported by default.

Learn NextJS Docs

like image 103
Agney Avatar answered Oct 23 '25 02:10

Agney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!