Search code examples
javascriptphpreactjsbrowserify

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?


Solution

  • 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.