简体   繁体   English

ReactJS-如何使用ES5路由视图

[英]ReactJS - How to route views with ES5

Most of the ReactJs guides that i've found are based in ES5 . 我发现的大多数ReactJs指南都基于ES5 So i've started to use ReactJS in this way. 所以我已经开始以这种方式使用ReactJS Now that i know how to create all my components it's time to create a little single-page-application . 现在,我知道如何创建所有components ,是时候创建一个小single-page-application My trouble is to understand how to route my views without reload the page. 我的麻烦是要了解如何在不重新加载页面的情况下route views I've find react-router ( https://github.com/rackt/react-router ) and this seems what i need for my aim, but all the examples are written in ES6 and currently it's not yet clear to me. 我已经找到了react-routerhttps://github.com/rackt/react-router ),这似乎是我实现我的目标所需要的,但是所有示例都是用ES6编写的,目前还不清楚。 Surely i'll refactor my app in ES6, but i'd prefer to follow the way i've started. 当然,我会在ES6中重构我的应用程序,但我更喜欢遵循开始的方式。

Is there someone who could help me to "translate" react-router to ES5 and let me get this clearer please? 是否有人可以帮助我将React-Router“转换”为ES5,并让我更清楚地了解它?

Here is an example for you from react-route tutorial 这是react-route教程中的一个示例

render((
  <Router>
    <Route path="/" component={App}>
      <Route path="about" component={About} />
      <Route path="inbox" component={Inbox}>
        <Route path="messages/:id" component={Message} />
      </Route>
    </Route>
  </Router>
), document.body)

Components App, About, Inbox, Message were created with ES6 syntax but it doesn't matter it also translates your ES6 syntax to the ES5 using babel, so you can easily pass there your ES5 components. 组件应用程序,关于,收件箱,消息都是使用ES6语法创建的,但这并不重要,它还可以使用babel将ES6语法转换为ES5,因此您可以轻松地将ES5组件传递到该组件。

Also, this two lines of code: 另外,这两行代码:

import { render } from 'react-dom'
import { Router, Route, Link } from 'react-router'

means: 手段:

var render = require('react-dom').render;
var Router = require('react-router').Router;
var Route = require('react-router').Route;
var Link = require('react-router').Link

I hope it will help you. 希望对您有帮助。

Thanks 谢谢

The previous answer forgot to mention about the BrowserHistory, which is an essential part of the ReactRouter. 先前的答案忘了提到BrowserHistory,它是ReactRouter的重要组成部分。 You can level up here . 你可以在这里升级。

var React = require("react");
var ReactDOM = require("react-dom");
var SchoolsList = require("./components/SchoolsList.jsx");

var Router = require('react-router').Router;
var Route = require('react-router').Route;
var Link = require('react-router').Link

var browserHistory = Router.browserHistory;



var _question = schoolsStore.getQuestion();

function render(){
    //console.log(_question);
    ReactDOM.render(
    <Router history={browserHistory}>
    <Route path="/" component={SchoolsList} question={_question.question} activeIndex={_question.activeIndex} />
    </Router>,
    document.getElementById("container"));
}

render();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM