php-to-react-data
Installation
SKILL.md
Passing Data from PHP to React
Simple History uses two main approaches to pass server-side data to React components. Choose based on when and where the data is needed.
Approach 1: wp_localize_script (Global Variable)
Data is available immediately on page load as window.variableName.
When to Use
- Data needed before any API calls complete (e.g. URLs for initial render)
- Static config that doesn't change during the session
- Data needed by components outside the React tree (e.g. FilteredComponent HOCs)
- Simple values like URLs, feature flags, nonces
Pattern
PHP (in the module that enqueues scripts):