Skip to content

Instantly share code, notes, and snippets.

@taniagreen
Last active September 8, 2021 21:38
Show Gist options
  • Save taniagreen/ac0723c6ef892e4140f9a4111195ee8a to your computer and use it in GitHub Desktop.
Save taniagreen/ac0723c6ef892e4140f9a4111195ee8a 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