Last active
September 8, 2021 21:38
-
-
Save taniagreen/ac0723c6ef892e4140f9a4111195ee8a 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