Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate React components in PHP and then render in client

I'm trying to use React to simplify the way I build UI for an app I'm writing. I've got a good proof of concept working in a single HTML page, but I need to split out the JS into individual modules to make it easier to maintain.

I know how I would structure the app if every piece of the puzzle is known at build time. Something like this: https://github.com/jordwalke/reactapp. However, the UI will be built up dynamically in PHP, nesting various components which I want to pass into the app.

My initial thought of dumping the generated code into a script tag and having the Javascript find it has led me into a horrible dependencies hell.

My primary question is:

How do I pass something like <panel><group><field /></group></panel> into the app so I can call React.render() on it?

like image 644
Josh Avatar asked Mar 05 '26 14:03

Josh


1 Answers

As @llernestal and @MikeDriver suggested in the comments, I used PHP to build a JSON representation of the UI and had React build the components needed to render the UI.

like image 68
Josh Avatar answered Mar 08 '26 04:03

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!