Welcome back, coding friends! Today, we're diving into the world of rendering components in React JS. By the end of this tutorial, you'll have a solid understanding of how to create, render, and manage components in your React applications. Let's get started! šÆ
Before we dive into rendering components, let's quickly review what they are. In React, a component is a reusable piece of code that represents a part of a UI. Components can be used to create simple widgets, complex interfaces, or even entire applications.
š Note: React components are pure JavaScript functions or classes that take inputs (props) and return a React element.
To create a new component, simply define a JavaScript function with a specific structure. Here's an example of a simple component called HelloWorld:
function HelloWorld(props) {
return <h1>Hello, {props.name}!</h1>;
}In this example, HelloWorld is a function component that accepts a props object. The component returns a React element, in this case, an h1 tag with the passed name property.
To render a component, we use the ReactDOM.render() function. This function takes two arguments:
Here's an example of rendering our HelloWorld component:
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<HelloWorld name="World" />);In this example, we first create a root for our React application using ReactDOM.createRoot(). Then, we render our HelloWorld component with a name property of "World". The component will be rendered inside the HTML element with the id of root.
Rendering multiple components is simple. Just pass multiple components as arguments to the ReactDOM.render() function:
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<>
<HelloWorld name="World" />
<AnotherComponent />
</>
);In this example, we render both the HelloWorld and AnotherComponent components. The <> tag is called the Fragment and is used to group multiple components without rendering an extra DOM node.
Components can be updated by changing the state or props. In React, the state is a built-in object that stores data for a component, and props are the data passed to a component from its parent. Updating the state or props triggers a re-render of the component.
Here's an example of updating the HelloWorld component based on a click event:
class HelloWorld extends React.Component {
constructor(props) {
super(props);
this.state = { name: 'World' };
}
handleClick = () => {
this.setState({ name: 'User' });
}
render() {
return (
<button onClick={this.handleClick}>
{this.state.name}
</button>
);
}
}In this example, we create a class component called HelloWorld with a constructor that initializes the state with a name of "World". We also define a click handler (handleClick) that updates the state with a new name of "User". In the render method, we create a button that displays the current state's name and triggers the click handler on click.
What is the purpose of the `ReactDOM.render()` function?
Hope you're finding this tutorial helpful! In the next part, we'll dive deeper into working with state and props in React components. Until then, happy coding! š”