简体   繁体   English

我应该使用快速,客户端反应路由器还是服务器端反应路由器?

[英]Should I use express, client-side react router or server-side react router?

I have a simple app which shows a list of comments by users. 我有一个简单的应用程序,显示用户的评论列表。 When a user is clicked the app should go to /users/<id> and show a new page with the users details that will be queried from a MongoDB. 单击用户后,应用程序应转到/users/<id>并显示一个新页面,其中包含将从MongoDB查询的用户详细信息。 Im having trouble to understand where should that logic be. 我很难理解逻辑应该在哪里。

I saw examples of using the react router in the client like: 我在客户端看到了使用react路由器的例子:

render((
<Router>
  <Route path="/" component={App}>
     <Route path="/user/:userId" component={User}/>
  </Route>
</Router>
), document.body)

But also like this in the server side: 但在服务器端也是如此:

<Route name="root" path="/" handler={require('./handlers/Root')}>

And also using express routing: 并且还使用快速路由:

app.get('/', function home (req, res, next) {
  res.render('layout', {
    reactHtml: React.renderToString(<App />)
  });
});

app.get('/user', function home (req, res, next) {
  res.render('layout', {
    reactHtml: React.renderToString(<User />)
  });
});

Which one is the way to go? 哪一个是要走的路? What are the differences? 有什么区别?

React applications are often initially client-side only, attached to the node as you have noticed. React应用程序通常最初只是客户端,如您所注意到的那样附加到节点。 This is what makes it so speedy: only API calls, no re-renders. 这就是它如此迅速的原因:只有API调用,没有重新渲染。

Isomorphic apps run on the server too, which is helpful for fallback (no JS) and for SEO and social sharing (Facebook needs to read the HTML meta tags. This isn't at all easy to achieve. You can also get really fancy and hydrate from the server rendering, which accelerates the user experience on those deeper pages. 同构应用程序也在服务器上运行,这有助于回退(没有JS)以及SEO和社交共享(Facebook需要阅读HTML元标记。这根本不容易实现。你也可以得到真正的幻想和来自服务器渲染的水合物,这加速了那些更深层页面上的用户体验。

What you will likely never want is to just render server-side. 您可能永远不会想要的只是渲染服务器端。 Kind of misses the full power of React. 有点想念React的全部力量。

Start client, with some good boilerplate like https://github.com/erikras/react-redux-universal-hot-example which will take you all the way to isomorphic and back again. 启动客户端,使用一些好的样板文件,如https://github.com/erikras/react-redux-universal-hot-example ,它将带您一路同构,然后再返回。

有了反应你做单页应用程序,所以你永远不会重新加载页面,快递你正在制作一个服务器像apache与PHP

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

相关问题 React-Router客户端 - React-Router Client-Side React-router服务器端错误 - React-router server-side error 使用React / redux和Express将客户端参数传递到服务器端(EMPTY_RESPONSE错误) - Passing client-side parameters to the server-side with React/redux and Express (EMPTY_RESPONSE error) 我应该在服务器端还是客户端渲染html? - Should I be rendering html on the server-side or client-side? 使用 React Router 的服务器端渲染“浏览器历史需要一个 DOM” - Server-side rendering with React Router "Browser history needs a DOM" 服务器端渲染与react,react-router和express - Server side rendering with react, react-router, and express 如何在服务器端访问 node/express 发送的数组并在谷歌地图的客户端使用它? - How to access an array sent by node/express at server-side and use it at the client-side in google maps? 导出服务器端功能以供客户端使用 - Export server-side function for client-side use 在服务器端进行React组件渲染,但客户端不会接管 - React component rendering on server-side, but client-side does not take over React 客户端获取的 URL 与服务器端请求的 URL 不匹配 - React Client-side fetched URL doesn't match server-side requested URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM