Current section: Raw React APIs 59 exercises
solution

Nesting Elements

Loading solution

Transcript

00:00 All right, so we want to make this experience or create this DOM using the React Create Element APIs. So what's interesting is we've got one element here, another element here, another element here, and then we've got this little space right here we've got to think about.

00:14 So like we said before, there is a children prop that we can pass here and we do, as our AI assistant is showing us, an array right there, but instead we're going to use the third argument. And the Create Element API accepts the first as the type of element you're creating.

00:34 The second argument is the props you want those to have, but then the children prop is kind of special. And so all the arguments following that will be the children all together. So you can just have as many additional arguments as you like. So we're going to replace the hello world with Create Element calls.

00:53 So we're going to create additional elements. First we're going to create a span, it has no props, so we'll just say null, or you could do an empty object there. And then hello, so we save that, we've just got hello, it's a span, perfect. But then we need to have a space. And the cool thing with the Create Element APIs, if you're just going to do a string

01:13 of text, you actually don't need to have a Create Element call, you can just pass the string of text and React will make sure that that becomes a text node for the string. And then we'll have another Create Element for the world. And there we go, we get hello world.

01:28 If we didn't have this extra string, then we're going to get those things bunched up together. So that's why that string space is important. The interesting thing is, you can't actually observe this difference in the HTML. So if I uncomment that, we save that, comment it again, like the HTML, or at least the DOM

01:46 view in the elements looks a little like it looks exactly the same, it doesn't actually look like it changes at all. And that's partly because of the way that the browser works with, or HTML works with spacing between elements.

02:03 If there's any number of spaces between elements, it's going to say, okay, that's just a single space. And of course, like different elements have different block and like display block versus display inline, all that stuff. But yeah, often, if you have two elements that need to be side by side, but they need

02:19 to be in a different element, you're going to want to have a space in there to be very explicit about, yes, I do want to have a space in there. And versus I want to have them right next to each other, there could be situations where you want that. And then of course, you have like all the styling and everything too, if you want to have an icon next to another thing, you probably don't want to do a space, you probably just

02:38 do like some padding or margin or something. So there you go. So this is creating elements with React. And if we actually console.log element, this will be interesting to you, except you got to spell it right. It doesn't work if you don't. There we go. So we'll go to our console and we'll take a look at the props.

02:57 You'll see that children, even though we pass these as additional props, the create element API is going to slurp those up into an array of children right here. And so we'll see, we have our nested elements right here with children of hello, the type span, we have our space, and then we have another one right here.

03:15 And then when we say create root and render that element, it's going to convert all of this stuff into a DOM so that we can render that to the page. And that is handling nested UI with the create element API from React.