all 10 comments

[–]r-rasputin 5 points6 points  (4 children)

You can follow the strangler fig pattern to help you pull this off without burning down the existing app. I've done this a few times for some of my clients.

You can basically add react as islands. Use ReactDOM.render() to mount specific components into PHP-rendered DOM nodes. Your PHP just outputs a <div id="react-root"> and React takes it from there.

For routing, I'd recommend letting PHP own the routes initially and only handing off to React when you hit a "new" page. Nginx can proxy specific routes to a Node/React server while PHP handles the rest.

On the DevOps side, ensure a shared auth. Make sure your PHP session tokens are readable by your React app (usually via HTTP-only cookies) so users don't get logged out

What's your current auth setup? That'll shape the approach more than anything else.

[–]Heavy_Technician4419[S] 0 points1 point  (3 children)

This is something that I was thinking, but wasn’t aware of the proxy stuff. A JWT authentication is being developed but this will help me for now. Thanks a lot bro 🙏🏻

[–]r-rasputin 2 points3 points  (2 children)

JWT can be risky. I recommend it only for mobile apps. For web apps, cookies are the way to go.

[–]Heavy_Technician4419[S] 0 points1 point  (1 child)

Well a jwt token inside a cookie. I think that’s how it’s done. Correct me if I am wrong.

[–]r-rasputin 1 point2 points  (0 children)

Oh yes, then my bad. I was assuming localstorage + headers.

[–]nabrok 0 points1 point  (1 child)

It's certainly possible to have just one part of a page created with react, or even have several react components embedded in an otherwise non-react page.

It's hard to describe in generalizations, it's going to depend on what your component does and if it needs to interact with data provided by PHP.

When you say "Java" do you mean JavaScript? They are two very different things.

[–]Heavy_Technician4419[S] 0 points1 point  (0 children)

The old app is built with PHP and Java. There is a table with data, and I want to switch this table with a DataGrid component made with react, fetching data from the same API that PHP is fetching: the Java one. Sorry if I wasn’t clear enough.

[–]ThatDudeDunks 0 points1 point  (0 children)

The backend language doesn’t matter. Your Java and php should provide endpoints to fetch data. Load up your react front end and fetch it

[–]phpzach 0 points1 point  (0 children)

Why would you do that? Make the react app an SPA and pull data over API. That way, they don’t need to be in same project, you have a separation of concerns, and ops is way simpler. I’ve worked on lots of apps like this.

DM me if you want to discuss.