简体   繁体   中英

Embedding a React App in an existing Rails App

The company I work for is currently experimenting with a newer version of their frontend. One piece of functionality is a self-contained React app that feeds off Rails APIs. We'd like to for one part of the app (the React part) use the react router instead of the rails router.

Is there any way for us to be able to embed the react app as its own section of the app whilst still maintaining sessions and being able to use the rails router for the rails components and the react router for the react components.

Yes this is a very common thing to do especially when migrating into more modern client heavy applications.

Like you said you will use rails to handle session management through cookies and routing will be the tricky part. You may however choose for rails to serve a single page and have react-router handle the rest. That is depending on things like auth see .

Once an html page has been rendered the javascript will load and react-router will handle the rest of the routing. This means that you will not be able to notify rails of client side route changes. It is difficult for me to address if that will be a problem without knowing more about your application design.

Also see this gem .

// Add react in existing app with yarn

yarn install
yarn add https://github.com/rails/webpacker.git 
yarn add react
yarn add axios 

resolved_paths: ['app/assets'] // Add it in webpacker to get access for assets file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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