Search code examples
reactjsroutesreact-routerrefresh

React JS refresh page every time clicking on menu item using route


I am new to React JS and I am currently building a simple application. I am using Route in order to navigate between components and everything work fine, but if I am on a page and I click again in the menu to navigate to the page, it doesn't refresh its content.

I just want the component to refresh its content every time I click on the item menu.

This is my sidebar class:

 class Sidebar extends Component {
constructor(props) {
    super(props);
}
render() {
    return (
        <Router>
            <Route render={({ location, history }) => (
                <React.Fragment>
                    <SideNav
                        onSelect={(selected) => {
                            const to = '/' + selected;
                            if (location.pathname !== to) {
                                history.push(to);
                            }
                        }}>
                        <SideNav.Toggle />
                        <SideNav.Nav>
                                <NavItem eventKey="Cars">
                                    <NavIcon>
                                        Cars
                                    </NavIcon>
                                </NavItem>
                                 <NavItem eventKey="Bicycles">
                                    <NavIcon>
                                        Bicycles
                                    </NavIcon>
                                </NavItem>
                        </SideNav.Nav>
                    </SideNav>
                    <main>
                        <Switch>
                            <Route exact path="/" component={props => <Home />} />
                            <Route
                                exact path="/Cars"
                                render={() => !isAllowed ?
                                    <Home /> :
                                    <Cars/>
                                } />
                            <Route
                                exact path="/Bicycles"
                                render={() => !isAllowed ?
                                    <Home /> :
                                    <Bicycles />
                                } />
                        </Switch>
                    </main>
                </React.Fragment>
            )}
            />
        </Router>
    )
}

}

This is my Cars Component class:

import React, { Component } from 'react';

class Cars extends Component {
    render() {
        return (
            <div style={{ textAlign: 'center', marginLeft: '295px' }} >
                <form>
                    <h1>Hello</h1>
                    <p>Enter your car name:</p>
                    <input
                        type="text"
                    />
                </form>
            </div>
        )
    }
}
export default Cars;

For ex. if I text something in input and after that I click on the item menu, I want that input to be refreshed.


Solution

  • In order to "refresh" (or in React world called Re-render) the content of the component you need to change it's state, and that is how React works. As I can see you don't have any state in your component so if you can specify what you wanna "refresh" we can help you.

    The heart of every React component is its “state”, an object that determines how that component renders & behaves. In other words, “state” is what allows you to create components that are dynamic and interactive.

    Quick example from somewhere on the internet :

     import React from 'react';
    
      class Person extends React.Component{
        constructor(props) {
          super(props);
          this.state = {
            age:0
          this.incrementAge = this.incrementAge.bind(this)
        }
    
        incrementAge(){
          this.setState({
            age:this.state.age + 1;
          });
        }
    
        render(){
          return(
            <div>
              <label>My age is: {this.state.age}</label>
              <button onClick={this.incrementAge}>Grow me older !!<button>
            </div>
          );
        }
      }
    
      export default Person;
    

    The age of inside of the label is being Re-rendered (or "refresh") every time when the user clicks on it since its state is changing.

    Here is an official documentation and I would recommend you read it, it will clarify a lot of issues you are facing.

    https://reactjs.org/docs/state-and-lifecycle.html