- Podcasts
- viewSource
- Getting started with React inside WordPress
Getting started with React inside WordPress
viewSource
- March 20, 2023
- 25 min
- Season 1, episode 8
About this episodeDig into what React is and how it works, especially inside WordPress, with Aurooba and Brian as they set up the basic environment and load…
Dig into what React is and how it works, especially inside WordPress, with Aurooba and Brian as they set up the basic environment and load up a little React component inside a WordPress post. Part one of a series of episodes where they'll transform their code into a working accordion block.
A full transcript of the episode is available on the website. Watch the video podcast on YouTube and subscribe to our channel and newsletter to hear about episodes (and more) first!
- The source code from the episode – https://github.com/viewSourcePodcast/viewSource-blocks/tree/add/accordion-block
- @wordpress/scripts – https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/
- Wes Bos' courses – https://wesbos.com/courses
- The "Learn JavaScript Deeply" moment in WordPress history – https://youtu.be/TX6dXRL-jPU?t=3536
- Brian's website – https://www.briancoords.com
- Aurooba's website – https://aurooba.com
- (00:00) - Introduction
- (00:26) - Learn JavaScript Deeply
- (02:59) - What is React?
- (06:37) - Comparing React to other familiar WordPress tech
- (08:36) - A React-ive "Hello World"
- (10:02) - @wordpress/scripts and the scary part of React
- (11:21) - Setting up a basic index.js in React
- (12:12) - React and @wordpress/element
- (14:09) - Outputting the React app with createRoot
- (18:27) - Exporting and importing functions in React
- (22:12) - Breaking your code up into smaller files
- (23:39) - Concluding Part 1!
- (24:21) - Outro
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Getting started with React inside WordPress
**Aurooba Ahmed:** Calypso. Yeah, so I remember seeing Calypso. I'm like, okay, are they thinking they're gonna bring something like that to Core? Oh, this is built in React. Okay, okay. Maybe it's gonna be React. So yeah, I bought the course, didn't go through it, until much, much later. But yeah. I wonder if Wes was like, what is happening? Why is this happening?
**Brian Coords:** Yeah. Yeah, and I think you and I have talked about the fact that there actually is not a great like WordPress JavaScripty course that I like I, I don't have one in my mind when people ask like, oh, I wanna learn JavaScript for WordPress. I don't have that in my head of where I would send them other than, you know, Wes' React course is a good, you know, a great place if you don't know react for sure.
**Brian Coords:** That the 30 day, like you do a little thing every day, that one is great. That should be like if you're kind of unsure about JavaScript or like you just have a little bit of , you should do that like once a year or something. Like it's just a fun little like 30 day challenge, don't eat carbs and do 30 days of JavaScript.
**Aurooba Ahmed:** But today we're gonna talk about, you know, React because that's the thing in WordPress and, uh, how React actually works. And hopefully this is gonna be a series where we start from like really, really basic and build what you called on Twitter, like the to-do list version, of WordPress blocks.
**Aurooba Ahmed:** Yes, in the JavaScript world that we find ourselves in now. Yeah. So, you know, we will build one over a course of a few different episodes, but before we actually build an accordion block, we actually have to understand a little bit of React. Like what is React? So, uh, what is react, Brian?
**Brian Coords:** This is my, my how I explain react in like beginner terms or something, the metaphor that I use, which is I think when I explain React, I think about Facebook, the company that owns React or like created React. I think about how Facebook, um, is sort of a, a user interface that everybody's fam pretty familiar with, you know, social media network.
Like social networks in general. So you have like a giant column of like latest feed information and you have like little avatars of people and you have some notifications. And what React is, is React is a way to say like, when I'm looking at all this stuff on a website, I want it to all be controlled in like one dynamic place.
And I want like all of those little components, the, the feeds, the posts, the things I want them to be as like performant and reusable and connected as possible. And that was like, super hard in the old days of kind of vanilla JavaScript and jQuery. But what React does is it basically says like, oh, this is a component, this is your component of messages. And when you read a message, the other component that says how many unread messages you have will automatically update and react to the fact that you up read this message over here somewhere else in your thing and this component will be here and you'll be reusable and all this sort of stuff.
**Aurooba Ahmed:** That's very accurate to how I would describe it, essentially, you know, it, it's, it's got a really good name. It tries to react to user interaction, and the way that it does that is React actually builds a sort of virtual DOM, so DOM being like your whole document in, in, in the browser, and it will build a React version of it and then it will track the comparison of what's happening in there versus what's actually happening in the html, in the actual DOM. So when you click something in the browser, it's gonna say, oh, this click, I don't have that tracked in my, you know, shadow DOM.
Okay, I need to update that. And then based on what React, like, you know, what you have programmed, it's gonna be like, okay, we need to add this into the HTML, so update the shadow DOM with what's supposed to happen. And that will, you know, compare to where the differences are in the HTML and then it'll interact there.
**Brian Coords:** I feel like it was the first time where it flipped it like the old jQuery days was like store everything in your HTML as like a little data attribute, like HTML nodes were just like getting packed full of data and you always had to update them and check them and like shift them around and stuff.
And react was like, no, no, no, like, we will handle all of the thinking and then we'll make the HTML after. And like, it's sort of like, you know, it's, it's sort of moving the source of truth and information kind of away and like keeping it in a nice, safe place where it can like feed little information down through all your little components.
**Aurooba Ahmed:** Yeah, exactly. And just like PHP where you kind of have your render like your output and your logic mixed together. But when you're writing a PHP like document, you can say, oh, all of my logic actually lives here at the top and all of my HTML lives at the bottom. React especially the way if you use JSX works kind of the same way where you can like have your logic kind of near the top of a function and then that function, it returns the HTML and that's where all of your, like anything that's gonna happen in the browser actually is written.
**Brian Coords:** Yeah. You know, react is like a very component based, so like a component for this and then inside of your component, there's little components for this and they're very reusable. And if you build WordPress themes and you've used like template parts, I feel like that's kind of a good way to think about it.
It's like you get your little template part and it kind of gets some stuff from like the global loop and it populates it in, but it's a reusable component. Um, if you've used a lot of template parts in your theme building, like that mental model of you know, breaking things into little components and, and passing information down through them is very similar except like also it's like very reactive and live and, you know, does, does like a million magical, like things without reloading the whole page.
**Aurooba Ahmed:** I get it. I'm between, I'm between browsers right now, I guess you could say. Okay, so on my screen I've got just a child theme of Twenty Twenty Three in WordPress, and there is a post, and I've called it "Building an Accordion" because over time we are going to build an accordion right here, but underneath it there is a button and it says hello.
**Brian Coords:** Yeah, I mean, I think for a lot of people, the scary thing with React is like I need to install a bajillion dependencies and like, you know, I need to make a big comp, you know, like build process and compile things and all this sort of stuff. And even like if you've ever used like vue.js where you kind of can skip all that and you can get up and running, which just like including it on the page, which is crazy.
**Aurooba Ahmed:** Yeah, that's true. Uh, the nice thing about WordPress is that we have this package that the Core maintains called @wordpress/scripts. And @wordpress/scripts takes care of a lot of this because of the fact that WordPress packages React into every WordPress instance. So React is available, you just have to start using it.
**Aurooba Ahmed:** Okay, so, uh, I've got my code editor open now and it's showing up on the screen. And the very first thing we're looking at is the index.js file. And this file is 15 lines long. Uh, but really the code part is 1, 2, 3, 4, 5, 6, 6 lines of code here, of which really. 1, 2, 3, 4 lines are required in order to put React, like React on your page in WordPress.
**Brian Coords:** And so when you decided to put this index.js file, you suck it in like a src folder and stuff like that. And the idea here is like if I'm using WordPress' build process, it's gonna do all of these things like the imports, there's like import this from this and import app from this and it's gonna do all of that for me and I don't even need to think about it.
**Aurooba Ahmed:** Yeah. You don't have to think about it. Like the @wordpress/element, which is the first package that I'm importing from, is basically how WordPress packages react into WordPress. The reasoning for this is in case they ever wanna switch out of React on, in underneath, behind the scenes, they can do that without you having to rewrite every single thing that you've ever written.
**Brian Coords:** So, so I'm importing from React, but like big picture, do you, how much, how do you keep track of like, I'm using React, I'm using vanilla JavaScript and I'm using WordPress' very specific flavor of React, or do you just not really think about that right now? Cuz you're just like, I'm in WordPress, that that's the most important thing to fun, to focus on.
**Aurooba Ahmed:** So I think this is something that a lot of people trip over when they're first getting into like WordPress block development especially because yeah, it's really hard to know what is React, what is WordPress React and what is like the WordPress block stuff. Everything that's inside Element, for the most part is straight up React from this package.
It's just React stuff that is being pulled in. It's not all of React, but it's most of what you might need from React. Those base things that React does and it allows you to pull from them. And today what we're gonna be looking at is just React. We're not looking at anything WordPressy flavored. We're not looking at, you know, what the WordPress Block API does in React.
All of that is, that comes later. And it's a lot like when people do PHP dev for the first time for WordPress, and they only know WordPress PHP. You know, they know how PHP works inside WordPress, but they don't necessarily know WordPress on its own. so it's a little bit like that when you start out, but like today, we're gonna look at just, just React. That's it.
**Aurooba Ahmed:** Okay, so createRoot is whenever you're using JavaScript, really, and even jQuery, or let's say you were using AJAX in the regular WordPress fashion, you need to create, you need to have a container or an element on the page that you target and say, here is where I want to inject my JavaScript. Or here is where I wanna create things, let's say with AJAX, you were like pulling posts dynamically from WordPress. This is the div that I want it to add it to. So that's what the root is like this is going to be the element that forms the foundation of where we're adding everything from React. It's the root of our setup.
**Brian Coords:** Okay, so in the build process, it's gonna grab that function and kind of like compile it into your JavaScript. So like, it, it pulls into everything you need. It imports this function that's gonna like, uh, let you create your root of where you're gonna kind of like, you know, render out your app and store all your data and all that sort of stuff.
**Aurooba Ahmed:** Exactly. And after that line I have something called import App. So in React, there's this best practice of having an index.js where you initialize something, but then you don't actually do your work in that file. You, you put all of that work in a different file. Often it's called app because React is often used to create applications, and so you have to import that into this index.js so you can insert it in the root that you're gonna make. Then I have, I'm just importing a stylesheet just to add some little styles to make it easy for us to look at.
We are on line 12 if you are following along and you have this repo, which we'll have in the show notes, uh, we're creating a constant called domNode because what we have to do is identify and get the wrapper, the div, where we want to actually insert our app, insert our React stuff. In this case, I'm identifying and looking for a div with, or an element essentially with an ID vs-accordion-root. And on the front end, I actually just took a group block in WordPress and I gave it the HTML anchor with this so that I can just insert it there.
**Brian Coords:** But like when you make React, like the initial piece of HTML is like, I need a div with an ID. Like, like is, is a lot of times what you're looking at. It's like, I need a div, I need an ID, and then I'm just gonna, like, that's gonna be the, the piece of DOM where I'm just gonna like, pummel, everything
**Aurooba Ahmed:** I mean, we're all used to doing that kind of thing with JavaScript in general, like whether it's jQuery or you're running like AJAX to pull posts from WordPress, it's a very common thing to have a wrapper or some sort of div that's empty, and that's where you put all of your, like the JavaScript stuff is gonna get added there.
Then what you do is now that you've found this element, you have to turn it into, uh, a place where you can add React and that's what the createRoot function does. It says, hey, this is going to be the element that I wanna transform into my react stuff. And once you've done that, you grab your application that you're importing and the, and the name I gave it is app, that's the variable for that. And then I just render it. I say, Hey, take this and put it into that root that we just created on that vs-accordion-wrapper wrapper, div.
**Brian Coords:** And so this, this right here is, is like from project to project, like this is pretty much like 80% of the time what you're gonna be looking at, right? I mean, like, you grab an element, you have an app, um, you import it, you render it. This part here is like, sort of like, I'm, I'm gonna let Copilot write this page for me in the future because it's always, this is gonna be pretty similar to this, but the app.js file, like that's where the actual React app that we're building is kind of gonna live.
**Brian Coords:** Yeah. I, yeah. I have like 50 things I want to dig into here, but let's, yeah, let's do it line by line. Cuz you're right, it's like, it's a lot of lines, a lot of this too is like scaffolding stuff, you know, declaring a function, exporting a function. Like that's the kind of stuff like, it's a line of code that again, like you're gonna copy paste, Copilot's gonna write, you're gonna do it so many times you do it in your sleep.
**Aurooba Ahmed:** Yeah, so I'm gonna skip the first line just for a second. The first import that we're doing, we'll come back to that. This whole application is just a function, and then I export this function just like I would in maybe in a regular JavaScript file, and then I'm importing it later. In this function, we have a return, and the return area is where we spit out the HTML that we want happening on the front end of our website. So that's where I'm adding my button. And it's also where I have a little conditional that says, Hey, when I click that button, show the word world in a paragraph element.
**Brian Coords:** Well. I will say you make it sound like writing a function and exporting it in one JavaScript file and then importing it in your index.js file. Like I, I will say, you make it sound like that's a thing that everybody does all the time, but I would push back a little bit and say like, these are the places where I think a lot of people who came from like jQuery and maybe some vanilla JavaScript, and they're looking at WordPress and they're looking at blocks and they're looking at the block editor, they're looking at React, and they're looking at "learn JavaScript deeply".
Like that right there. I would bet that a lot of people would probably agree with me and say like, I don't know if that's JavaScript. I don't, that's like, whatever is it ESNext or what? Like that's, that's modern future JavaScript and like, that's already a little bit of a you know, so I, I'm just pushing you on that because I think it's
**Aurooba Ahmed:** That's fair. That's that's fair. And it's something that in jQuery, you often really don't need to do because you just write your function and then you include the file and it just works, you're right. Um, but yeah, like in a lot of JavaScript and especially in React, if you wanna take a file and include its contents in another file, you have to say that this is something I want to use outside of this file.
So you have to export it. And so here we've write written a function, and then we're doing something called export default and then the name of the name of that function, which is also something that can be confusing because this export default is assuming that there is only one function in this whole, uh, file that you want to be made usable outside of it.
If I had more than one function in this file and I wanted to export those, I would not say export default App. In fact, I would just say export and add that to the beginning of the function name. So it'd be like export function App, and then I could have multiple functions that I'm exporting. It's a lot like partials in SASS.
That's, I think, like the closest comparison to something that we already do. Right? Like you, uh, you don't have to export, but you do have to import things in SASS where you write a partial and then you import it somewhere. And if you didn't import it, then you can't use it. You know, variables, for example.
**Brian Coords:** And, and, uh, or another one is like in PHP, like I love my PHP to be like 50 files instead of one giant file. Like I love tiny little files that with different names and so, you know, it's a similar thing where you're requiring or including a file in PHP. There's not that like export process, but it's that same thing of like, how can we start breaking things into smaller files and, and if you're used to writing like one JavaScript file for your theme, and it's like 4,000 lines long, it's like a bunch of jQuery event listeners or whatever, you know, hey, we've all done it like that.
This is, you know, once you do realize like, oh, I can just write a function and I can export it, and then later when I need it, I can import it. And, and similarly, WordPress has written tons of JavaScript that they're exporting that we'll get into the, you know, like we did before, we can import it at the top and import that function that you need.
**Aurooba Ahmed:** Yeah, definitely. I think the practice of breaking things into little files instead of doing all of your work in long files is really, really important as you start to do more complex and intense things and JavaScript, especially block development is just by default, a complex thing, you know? So the more files you have, the cleaner and easier you make your code to read and navigate.
**Brian Coords:** Yeah, I think let's stop here. Let's come back. I think there's things we need to, like, we still need to talk about the, you know, state management we need to talk about like JSX and how all that works. We need to talk about all these like, weird, modern JavaScript things like destructuring and like, uh, spreading arrays and like all those like fancy things that kind of trip people up.
