App.js in the code
import React, {Component , Fragment} from 'react';
import {withRouter , Route , Switch , Redirect} from 'react-router-dom';
import './App.css';
import 'antd/dist/antd.css'
import Home from './component/Home';
import List from './component/List';
@withRouter
class App extends Component {
render() {
return (
<Fragment>
<Switch>
<Route path='/homePage' component={Home}></Route>
<Route path='/listPage' component={List}></Route>
<Redirect to={`/homePage`}/>
</Switch>
</Fragment>
);
}
}
export default App;
Running project error:
←→1 of 2 errors on the page
You should not use <Route> or withRouter() outside a <Router>
▶ 26 stack frames were collapsed.
./src/index.js
C:/Users/abcd/WebstormProjects/ant-table-demo/src/index.js:7
4 | import App from './App';
5 | import registerServiceWorker from './registerServiceWorker';
6 |
> 7 | ReactDOM.render(<App />, document.getElementById('root'));
8 | registerServiceWorker();
9 |
10 |
View compiled
▶ 6 stack frames were collapsed.
This screen is visible only in development. It will not appear if the app crashes in production.
Open your browser’s developer console to further inspect this error.
The solution
Package the App components via Route, as follows:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import {BrowserRouter, Route} from 'react-router-dom'
const Root = () => {
return (
<BrowserRouter basename='/'>
<Route path={`/`} component={App}></Route>
</BrowserRouter>
)
}
ReactDOM.render(<Root/>, document.getElementById('root'));
Then it will run correctly
Read More:
- React bootstrap loading and how to use modules
- An error is reported when using react app rewired to start the react project
- webstorm npm install –save –save-exact –loglevel error react react-dom react-scripts has failed.
- react Error: Objects are not valid as a React child (found: object with keys {username, password})
- [W xx:xx:xx.xxx NotebookApp] 404 GET/static/components/react/react-dom.production.min.js (::1)
- react Error: Unable to resolve module mobx-react
- Failed to load plugin react: cannot find module ‘eslint plugin react’ appears when running Vue project‘
- React Error: Minified React error #119
- The echo introduced in react reports an error: xaxis “0” not found
- nbconvert failed: xelatex not found on PATH, if you have not installed xelatex you may need to do so
- How to use dangerously set inner HTML in react
- [Vue warn]: Do not use built-in or reserved HTML elements as component id: article
- [Solved] VUE Error: “You may use special comments to disable some warnings.Use // eslint-disable-next-line to …
- Angular Error You seem to not be depending on “@angular/core“ and/or “rxjs“. (Fixed)
- Minifiedreacterror — react error report notes
- Click the button to use in the react project window.open Methods open a new page and click the data again
- Higher order components in react
- 【.Net Core】using declarations‘ is not available in C# 7.3. Please use language version 8.0 or greate
- 【react】 tsconfig.json Configuration of react JSX error reporting solution in