简体   繁体   English

如何使用react-router创建单页应用程序

[英]How to create a Single Page Application using react-router

I am quite new to React.js but I'm building a single page application using it. 我是React.js的新手,但我正在使用它构建一个单页面应用程序。 For routing I am using react-router. 对于路由我使用react-router。

The way I want to organize my page is as following: I want to have an static header and footer(loaded only ONCE), and then the content of the page that will change depending on the route. 我想组织我的页面的方式如下:我想要一个静态页眉和页脚(仅加载ONCE),然后页面的内容将根据路由而改变。 Nothing special here, this is basically my understanding of SPA. 这里没什么特别的,这基本上是我对SPA的理解。

This is the main.js that bootstraps my app with the routes: 这是使用路由引导我的应用程序的main.js:

const store = configureStore();
ReactDOM.render(
<Provider store={store}>
    <Router history={history}>
        <Route path="/" component={App}>
            <IndexRoute component={Home}/>

            <Route path="shop-filters-left-3cols" component={ShopFiltersLeft3Cols}/>
            <Route path="about" component={About}/>
            <Route path="login" component={Login}/>

        </Route>
    </Router>
</Provider>,
document.getElementById('main')
);

And this is the component App which is basically containing the structure of the single page: 这是组件App,它基本上包含单个页面的结构:

export class App extends React.Component {

constructor(){
    super();
    this.render = this.render.bind(this);


}

render() {
    console.log('Rendering App');

    return (<div>
        <LoginModal />
        <Header/>
        <div className="page-content">
            {this.props.children}
        </div>
        <Footer/>
    </div>);
}

}

So when I access the app, the component 'App' is loaded, and then the component 'Home' in injected into the main content as it is declared as IndexRoute. 因此,当我访问应用程序时,会加载组件“App”,然后将组件“Home”注入主要内容,因为它被声明为IndexRoute。

Everything is fine until here, the problem is that every time I switch from a route to another, the component 'App' gets rendered( I know it because I see the 'Rendering App' log in the console). 一切都很好,直到这里,问题是每次我从一个路由切换到另一个,组件'App'被渲染(我知道它,因为我在控制台中看到'渲染应用程序'日志)。 And this is what I don't understand, to my understanding, switching between routes shouldn't trigger a render of the 'App' component but only of the component that is being injected dynamically. 这是我不明白的,根据我的理解,路由之间的切换不应该触发'App'组件的渲染,而只触发动态注入的组件。

I don't want the header and footer to be rendered every time I switch a route, this is IMHO the benefit of having a single page application. 我不希望每次切换路由时都会呈现页眉和页脚,这是恕我直言,这是一个单页应用程序的好处。 Maybe I'm not understading well how react-router works, could someone please help me with that? 也许我没有充分理解反应路由器是如何工作的,有人可以帮助我吗? Or if I'm doing something wrong please tell me what is the right way to achieve what I need. 或者,如果我做错了什么,请告诉我实现我需要的正确方法是什么。

If you need to see any other code don't hesitate to ask. 如果您需要查看任何其他代码,请不要犹豫。 Thank you 谢谢

The App component is re-rendered because you said it was the root of the view: App组件被重新渲染,因为您说它是视图的根:

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

This is not a problem . 不是问题 React is designed to re-render Components , and you should write yours with that in mind. React旨在重新渲染Components ,你应该记住你的Components

Why is not a problem? 为什么不是问题?

Unlike most templating systems out there, what React calls rendering does not necessarily involve DOM changes . 与大多数模板系统不同,React调用渲染不一定涉及DOM更改 When React "re-renders" a Component , it computes the minimum set of operations that will take the DOM from its current state to its desired state. 当React“重新呈现”一个Component ,它会计算将DOM从其当前状态转换到所需状态的最小操作集。 This may be an empty set. 这可能是一个空集。

In other words, for your Components that produce the same HTML, nothing will be actually done . 换句话说,对于生成相同HTML的Components实际上不会做任何事情 Your header and footer probably fall into this category. 您的页眉和页脚可能属于此类别。

This makes it very fast. 这使它非常快。 If performance is your concern, you can safely push it out of your mind for everything except the most demanding computational scenarios. 如果您关注性能,除了最苛刻的计算方案之外,您可以安全地将其从脑海中推出。

If you really want to , you can avoid the re-rendering of your Component by implementing the shouldComponentUpdate() method. 如果您真的想要 ,可以通过实现shouldComponentUpdate()方法来避免重新呈现Component

This is not encouraged: React is moving towards stateless, functional components , and you should try to use them whenever possible. 不鼓励这样做:React正朝着无状态的功能组件发展 ,你应该尽可能地尝试使用它们。 Your job in this case is to guarantee that the same props will render the same HTML . 在这种情况下,您的工作是保证相同的props将呈现相同的HTML React's magical engine will do the rest. React的神奇引擎将完成其余的工作。

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

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