• Introduction Now that we’ve learned about JSX, let’s move on to Rendering Elements in React. Rendering elements is the process of displaying them on the screen. Geek and Gal are here to guide you through this important concept in a fun and engaging way!
Gal Normal

Ready to learn about rendering elements in React?

Geek Curious

Absolutely! What's the first thing I need to know?

Gal Happy

Great! The first thing you should know is that to render a React element, you need a DOM container to place it in!

  • Rendering Elements
Gal Pleased

Let's say you have an HTML file with a <div> element that has an ID of "root." This will be our container where we'll render our React elements:

<div id="root"></div>
Geek Curious

Alright, we have a container. How do we render a React element inside it?

Gal Happy

We use a function called ReactDOM.render() . You pass two arguments to this function: the React element you want to render, and the DOM container where you want to place it. Here's an example:

const element = <h1>Hello, world!</h1>;
const container = document.getElementById('root');
ReactDOM.render(element, container);
Geek Happy

I see! So, we create a React element with JSX, get the DOM container, and then use ReactDOM.render() to render the element in the container!

Gal Happy

Exactly! You got it! 😄

  • Hilarious Rendering Example
Geek Curious

How about a hilarious example of rendering an element?

Gal Pleased

Sure, let's render a funny message that changes based on the user's name!

const name = 'Alice';
const message = `Hey ${name}, why don't scientists trust atoms? Because they make up everything!`;
const element = <h1>{message}</h1>;

const container = document.getElementById('root');
ReactDOM.render(element, container);
Geek Happy

Haha, that's awesome! I love learning with humor!

Gal Happy

Glad you're enjoying it! Keep practicing, and you'll be a rendering expert in no time! 😄

  • Conclusion Rendering elements in React involves creating a React element with JSX, selecting a DOM container, and then using the ReactDOM.render() function to display the element on the screen. Have fun rendering your React elements and creating amazing apps! 😃