How to pass parameters from php to React component
Curious how to get some Wordpress post data to use in your ReactJS container?
I can see 3 options so far: the SPA one, the lazy one and the favourite one.
- HTTP request
If you live in a perfect world without legacy code, you can get data from server via HTTP request. In real life, without having dedicated API back-end, you need to opt for another solution.
2. Global variables
What can be simpler? Just stuff your global space with a new variable! Kidding. Try to avoid using global vars as access to them is slower than to the local ones and all the users have access to global data (are you sure you want to expose it?).
3. Use data-* attributes
I love this solution! Just add your parameters to data-* attributes and access them in the app endpoint through dataset property. Nice and clear.
Know another option? Feel free to share in a comment.