Search code examples
reactjsreact-routerreact-router-v4react-router-domreact-router-component

go back button doesn't navigate back


My code for App.js from where all the routing is done

  import React from 'react';
    import ReactDOM from 'react-dom';
    import $ from 'jquery';
    import {FirstPage} from './FirstPage.js';
    import {Panorama} from './Panorama.js';
    import {BrowserRouter,Route,Router,Switch} from 'react-router-dom';
    var BrowserHistory = require('react-router/lib/BrowserHistory').default;
    
    class App extends React.Component{
        constructor(props){
          super(props);
        }
    
        render(){
          return(
            <div>
              <BrowserRouter history={BrowserHistory}>
                <Switch>
                  <Route exact path="/" component={FirstPage} />
                  <Route path="/panorama/:id" component={Panorama} />
                </Switch>
              </BrowserRouter>
            </div>
            )
        }
      }
    
    ReactDOM.render(<App/>,document.getElementById('container'));

FirstPage.js

import React from 'react';
import ReactDom from 'react-dom' ;
import $ from 'jquery' ;
import {Panorama} from './Panorama.js';
import {Redirect,Link} from 'react-router-dom';
import {withRouter} from 'react-router';

class FirstPage extends React.Component{
    constructor(props){
      super(props);
      this.state={
        list:[],
        images:[],
        isClicked:false,
        redirect:true,
        imageUrl:''
      }
      this.loadImages=this.loadImages.bind(this);
      this.loadOne=this.loadOne.bind(this);
    }
    componentDidMount(){
        window.addEventListener('load',this.loadImages);
   }

   loadImages(){ 
      //console.log("load");
      var that=this;
      $.ajax({
        type:'GET',
        url:'https://demo0813639.mockable.io/getPanos',
        datatype:'jsonp',
        success:function(result){
          var images=that.state.images;
          for(var i=0;i<result.length;i++){
            that.state.images.push({"pano":result[i].pano,"name":result[i].name});
          }
          that.setState({
            images:images
         })
        }

      })
   }

   loadOne(pano){
    console.log("pano: ",pano);
    let imageUrl=encodeURIComponent(pano);
    this.setState({
      isClicked:true,
      imageUrl:pano
    })
    this.props.history.push(`/panorama/${imageUrl}`)
  }

  render(){
    var list=this.state.list;
    console.log("Image URL: "+this.state.imageUrl);
     list=this.state.images.map((result)=>{
        return(<div className="box">
                <div className="label">{result.name}</div>
                  <img src={result.pano} className="image col-md-3" onClick={this.loadOne.bind(this,result.pano)}/>   
              </div>
              )

       })

    return <div>{list}</div>
  }
}

module.exports={
  FirstPage:FirstPage
}

This page loads a few images on the screen and when clicked on any of those, it goes into full screen mode,i.e. an image and a go back link now come on the screen.

import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
import {Link} from 'react-router-dom';

class Panorama extends React.Component{
  constructor(props){
    super(props);
    this.goback=this.goback.bind(this);
  }
  goback(){
    this.props.history.push('/');
  }
    render(){
        let url=decodeURIComponent(this.props.match.params.id);
      return( 
        <div className="pano">
              <img id="myImage" src={`${url}`} crossorigin="anonymous"/>
          
          <div className="goback" onClick={this.goback}>Go back</div>
          </div>
        )
    }
  }


module.exports={
  Panorama:Panorama
}

I am running the code on localhost:8080. When I click the go back button the url changes to localhost:8080 again but displays an empty page and I want FirstPage.js to be displayed. While debugging, I found that initially my html elements has 4 div elements as are written in the code of FirstPage.js Index page

But after I click the button, it navigates to localhost but the div elements become empty. after click

There are no errors in the console and the version of react-router is v4.


Solution

  • 'load' event is fired only once as the opened window no longer has the load event applied by the opening window. Please update componentDidMount in FirstPage.js to :

    componentDidMount(){
      this.loadImages()
    }
    

    You should avoid changing the state as below :

    for(var i=0;i<result.length;i++){
      that.state.images.push({"pano":result[i].pano,"name":result[i].name});
    }