React Js has become an essential tool in modern web development due to its efficient and scalable approach to building user interfaces. In this article, I will dive into the core concepts of React Js, including components, JSX, props, lifecycle methods, and event handling.
Components are the fundamental building blocks of a React application. They are reusable, self-contained pieces of code for rendering a specific UI part. React components can be either class-based or function-based. Function-based components are becoming more prevalent due to the introduction of React Hooks.
Below is an example of a simple function component. The component represents an html input with a label. Then I can use that component in forms where I expect user input.
const Input = (props) => {
return
<>
<label for={props.inputId}>{props.label}:</label>
<input id={props.inputId} name={props.inputName} type="text" />
</>
}
Note that the fragment
To use this Input component in another component, you would import it and include it like so:
import Input from './path/to/Input';
const RegistrationForm = () => {
return (
<form>
<Input inputId="name" inputName="name" label="First Name" />
<Input inputId="address" inputName="address" label="Address" />
</form>
)
}
There are several restrictions to follow when creating a React Js component. At this point, It is important to note the following. I will explain more later in the article.
JSX is a syntax extension for JavaScript that allows us to write HTML-like code within your JavaScript code. JSX makes writing and understanding component structure easier, and it is transformed into JavaScript during the build process. In my above example, I returned a JSX element.
<>
<label for={props.inputId}>{props.label}:</label>
<input id={props.inputId} name={props.inputName} type="text" />
</>
When returning a JSX element in React, there are some restrictions and guidelines to follow. I will explain most of them when I explain the code examples.
In React, data is passed from parent components to child components through "props" (short for properties). Props are read-only, meaning a component should never modify its props.
Here's an example of using props:
const Input = (props) => {
return
<>
<label for={props.inputId}>{props.label}:</label>
<input id={props.inputId} name={props.inputName} type="text" />
</>
}
const RegistrationForm = () => {
return (
<form>
<Input inputId="name" inputName="name" label="First Name" />
<Input inputId="address" inputName="address" label="Address" />
</form>
)
}
In the above example, I re-use the
Lifecycle methods are special methods in React components. They allow us to execute code at specific points during the component's lifecycle. The component lifecycle includes mounting, updating, and unmounting.
With the introduction of React Hooks, lifecycle methods can be replicated in function components using the
Here's an example of using lifecycle methods
const RegistrationForm = () => {
useEffect(() => {
console.log('Executes everytime the component updated')
})
return (
<form>
<Input inputId="name" inputName="name" label="First Name" />
<Input inputId="address" inputName="address" label="Address" />
</form>
)
}
I will explain the lifecycle hooks in great detail in the upcoming article.
In React, event handling uses event listeners attached to DOM elements. Event listeners are usually defined as functions in function components.
Here's an example of event handling: