react-router-dom : updates url but does not render the new component

user1462617

I am a bit lost with this issue for a whole day. On button click the url changes but does not render the new page and I don't understand why.

I am using react-dom-router 5.2.0

                       INDEX JS

 import {Router} from 'react-router-dom';
 import history from './history';

ReactDOM.render(
  <React.StrictMode>
    <Router history={history}>
      <App />
    </Router>
</React.StrictMode>,
  document.getElementById('root')
);
                           APP JS

import Server from './Server';
import React from 'react';

function App() {
  return (

   <Server />

  );
}

export default App;
                           SERVER JS

export default class Server extends Component
{
render()
    {


        return(


            <div className="Homepage" >

                <h1 className="header">Server</h1>



                     <button  className="button" 
                    onClick={() => history.push('/control')}>
                            Lets go
                    </button>

                }

      </div>

        );  
    }
}

Please Note : I added <Control/> directly in the render method above and it renders the component all well .

                        CONTROL JS

import React, {Component} from 'react';
import Page2_View from './Page2_View';

export default class Control extends Component
{
    constructor(props)
    {
        super(props);
    }

    render()
    {
        return(

            <Page2_View/>

        );


    }

}
                    Page2_View

import React, {Component} from 'react';

const Page2_View = (props) =>
{
    return(

        <h1> PAGE 2 VIEW </h1>

    );
}

export default Page2_View;
         ROUTES JS

import {BrowserRouter as Router, Route, Redirect, Switch} from 'react-router-dom';
const Routes = () =>
{
    return(

            <Router>
                <div>
                    <Switch>
                        <Route exact path="/test" component={Server}/>
                        <Redirect from = '/test' to = '/control'/>
                        <Route exact path="/control" component={Control}/>
                    </Switch>
            </div>
            </Router>

    );

}

export default Routes;
                          HISTORY JS

import {createBrowserHistory as history} from 'history';

export default history();

I appreciate all the help. Thank you

user1462617

I have realized what I was doing wrong and was able to solve my issue.

The key was to understand that the Router module from react-router-dom comes with three props : path , history, and component.

So in order to redirect a page on button click all I had to do embed all my Routes between tag in the App.js

                     APP JS
function App() {
  return (
  <Router>
        <Switch>
            <Route exact path="/test" component={componentA}/>
            <Route exact path="/test2" component={componentB}/>

        </Switch>
</Router>

  );
}

export default App;

And then you can use button onClick to redirect

                               COMPONENTA JS
<button variant="secondary" 
                        className="button" size="lg" 
                        onClick={() => this.props.history.push('/test2')}>
                         RedirectTo
                     </button>

Hope this will be helpful to others who come across this!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React Router url updates but component does not

React-route-dom updates url but doesn't render component

React Router does not render new component on Route of child component

React-router-dom v4 does not render the component

React Router does not render Component

React-router v4 updates url but doesn't render component

React does not render new states at their updates

URL changes but component is not render - React Router

React Router Redirect does not render the component

React Router changes route but does not render the component

React-router-dom v6, URL changing but component doesn't render

React router dom v5 history.push does not render new page

How to use React Router to route to another url AND re-render page with NEW component?

How to add a route that will render a component using react-router-dom?

Render Same Component With Multiple Paths React Router Dom

React-Router-Dom 6 - How to dynamically render a component?

React Router Dom, render a component only for some routes

Passing react-router-dom Link component as a render prop is failing

Component does not render when url loads in react-router v4

Low-level Router from React-Router-Dom only navigates to new URL but doesn't render components

React URL changes but component does not render

React Testing Library render does not work with react router dom Navigate

How do you access url args in react-router-dom 4 when using Route render prop instead of Route component prop?

React router: new component only render on existing view

React router does not render starting page component properly

NotFound component does not render in Router

react router dom is not updating class component when url change

React component does not render

React component render is called multiple times when pushing new URL