-
-
Save DustyReagan/7ee5f26fe149068f2e11e6c3b50a10ae to your computer and use it in GitHub Desktop.
Pass parameters from PHP to React component - global variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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