反应路由器 - 屏幕上没有任何内容

时间:2017-08-08 07:35:24

标签: javascript reactjs react-router react-router-v4 react-router-dom

我有一个代码,可以在屏幕上加载四个图像,点击其中一个图像,它会在屏幕上显示整个图像。出于导航的目的,我使用的是react-router。代码如下: App.js

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';

class App extends React.Component{
    constructor(props){
      super(props);
    }

    render(){
      return(
        <div>
          <BrowserRouter>
            <Switch>
              <Route path="/" component={FirstPage} />
              <Route path="/image/:id" component={Panorama} />
            </Switch>
          </BrowserRouter>
        </div>
        )
    }
  }

ReactDOM.render(<App/>,document.getElementById('container'));

进行ajax调用的FirstPage.js

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

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){
    this.setState({
      isClicked:true,
      imageUrl:pano
    })
  }

  render(){
    var list=this.state.list;

    return this.state.isClicked?<Redirect to={`/image/${this.state.imageUrl}`}/>:
        <div> {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>
              )

       })}
       </div>
  }
}

module.exports={
  FirstPage:FirstPage
}

用于在屏幕上显示单个图像的Panorama.js

import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
import 'aframe';
import 'aframe-particle-system-component';
import {Entity, Scene} from 'aframe-react';
import {Link} from 'react-router-dom';

class Panorama extends React.Component{
    render(){
      return( 
        <div>
          <img src={props.match.params.id}/>
          </div>
        )
    }
  }

module.exports={
  Panorama:Panorama
}

问题是屏幕上没有任何错误,但仍然在点击图像后,图像没有在屏幕上呈现。我在这里做错了什么? 我使用的react-router的版本是v4。 Before click after click

2 个答案:

答案 0 :(得分:0)

我的意见是修复路由器 从

<Route path="/" component={FirstPage} />

<Route exact path="/" component={FirstPage} />

通过设置它,它将阻止任何包含/的路由器呈现FirstPage

答案 1 :(得分:0)

在设置状态后,您可以使用Redirect方法动态路由到URL,而不是在设置状态后使用history.push。另外Panorama组件是statefull组件,因此道具可以像this.props那样使用。但是我没有看到你有任何逻辑,所以将它作为一个功能组件更好

使用history.push() 动态路由 this answer

export const Panorama = (props) => {
      return( 
        <div>
          <img src={props.match.params.id}/>
          </div>
        )
  }

代码:

import {withRouter} from 'react-router';
class FirstPage extends React.Component{
    constructor(props){
      super(props);
      this.state={
        list:[],
        images:[],
        redirect:true,
      }
      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){
       this.props.history.push(`/image/${pano}`)
   }

  render(){
    var list=this.state.list;

    return <div> {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>
              )

       })}
       </div>
  }
}

export withRouter(FirstPage);