Skip to content

Instantly share code, notes, and snippets.

@taniagreen
taniagreen / blablabla.js
Last active December 23, 2019 11:32
Pass parameters from PHP to React component - data-* attributes
/// userPage.php ///
// define your parameters
$post_id = get_the_ID() ?? null;
$post_title = get_the_title() ?? '';
// add parameters as data-* attributes
return "<div data-post-id={$post_id} data-post-title={$post_title} class=\"userApp\"></div>";
/// App.jsx ///
@taniagreen
taniagreen / blablabla.js
Last active September 8, 2021 21:38
Pass parameters from PHP to React component - global variables
/// user.php ///
// initialize global variable in a script added to your php file
<script>
window.reactData = {
postId: <?php echo get_the_id(); ?>
};
</script>