Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference using react-router and the express routes.js

I am doing a project using react, redux and express, I don't understand what is the difference between react-router and the express routes.js, did I need to combine the two or just use one ?

https://github.com/reactjs/react-router

Thanks for the help :)

like image 519
fandro Avatar asked Mar 04 '16 12:03

fandro


People also ask

What is the difference between Router and route?

In a WAN, routers serve as connection points between sub-networks, more generally referred to as nodes. Routing between sub-networks is guided by a routing table maintained in each end-system. The routing table points to the next device along a route for a packet to take in order to reach a given address.

What is the difference between Express and react?

Express is a minimal and flexible node. js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications; React: A JavaScript library for building user interfaces.

What is the difference between React Router Dom and react Router?

The react-router library includes all the features and custom components from react-router-dom and react-router-native libraries. You can think of it as a broad library that unites two more specific libraries. The react-router-native is built for React Native, a framework for building mobile applications in React.

Why is routes used in react Router?

ReactJS Router is mainly used for developing Single Page Web Applications. React Router is used to define multiple routes in the application. When a user types a specific URL into the browser, and if this URL path matches any 'route' inside the router file, the user will be redirected to that particular route.


1 Answers

Note: this stackoverflow post contains examples and codes that could help you a lot.

It's a classical misunderstanding. Express will handle your backend routes whereas React (with react-router or any front-end routing lib) will handle frontend routes. Your React application will probably be an SPA (single page application), meaning that your server (express or something else) will have to serve the index.html and react will handle your application from here. Which mean that React will evaluate the routes and decide which view to render.

Therefore, you have to ensure that when a users goes on a route like /accounts/me, the servers serves your frontend (react) application if needed, but something like /api/users/me would render data. It's just an example.

A "normal" usage would be to handle your data (via an API) with express and the application (pages and views) only with React.

If you are using server-rendering, it becomes a bit more complicated.

In most cases, yes, you will have to use both.

Edit: it would be easier to answer if your question was more specific about your usage and what you want to do.

Edit 2: Most of the time, it's not the same servers serving the frontend application and the API (data), if it is, just ensure that the application is send when some routes hit the serve: i.e /home, /about (which are obviously -here- not api routes) should be send serve index.html as your frontend application, and React will take care of the routes to decide what to render.

like image 51
Cohars Avatar answered Sep 26 '22 12:09

Cohars