Skip to content

Instantly share code, notes, and snippets.

@DustyReagan
Forked from taniagreen/blablabla.js
Created September 8, 2021 21:38
Show Gist options
  • Save DustyReagan/7ee5f26fe149068f2e11e6c3b50a10ae to your computer and use it in GitHub Desktop.
Save DustyReagan/7ee5f26fe149068f2e11e6c3b50a10ae to your computer and use it in GitHub Desktop.
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>
/// userContainer.jsx ///
class User extends React.Component {
constructor(props) {
super(props);
this.postId = reactData.postId; // here it is!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment