-
Notifications
You must be signed in to change notification settings - Fork 4
React Typescript useState English Detailed
Typescript React is a widely popular framework for building user interfaces for web applications. As you learn to program using Typescript React, one of the most important concepts to understand is something called a State
as it allows for dynamic rendering of content on the screen.
In functional components, useState
is a built-in Hook offered by React. This hook allows you to add state functionality to functional components and gives the ability to control and update the state of your application from within the component.
In this article, we will explore the concepts of State
, component
, props
, set
, and rendercycle.
State
is an object in React which decides how the component renders and behaves. A State
object holds some information that may change over the lifetime of the component and it is how React is able to manage the rendering of these changes on the screen.
An important thing to note is that state should only hold values that will change during the component lifecycle. Components may have multiple state properties, each of which are independent and can be changed separately.
import React, { useState } from 'react';
const MyComponent: React.FC = () => {
const [count, setCount] = useState<number>(0);
return (
<div>
<h1>Count: {count}</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
In this example, we declare and initialize our state variable count
to a value of 0
. We also declare a function setCount
to update the value of count. We use useState
to create this state variable. We pass the initial state value to useState
wrapped in an array with the first element representing the state's value and the second element representing the function to update the state.
A component
in React is a modular section of UI that can be reused throughout your application. In Typescript React, a component can either be functional or class-based.
In functional components, state can be added using the useState
Hook, which was discussed above.
import React, { useState } from 'react';
const MyComponent: React.FC = () => {
const [name, setName] = useState<string>('John Doe');
return (
<div>
<h1>Hello, {name}!</h1>
</div>
);
}
In this example, MyComponent
is a functional component that takes no props but has a state variable name
that defaults to 'John Doe'. When the component is rendered, the value of name
will be displayed on the screen.
Props
is short for properties. A props object is an immutable collection of data that is passed from parent to child component. These values are passed down as variables and are used to configure the child components based on the parent's needs.
Parent components can pass props to child components through the assignment props
. In the example below, the parent is passing the prop name
to the child component Greeter
.
interface GreeterProps {
name: string;
}
const Greeter: React.FC<GreeterProps> = ({ name }) => {
return <h1>Hello, {name}!</h1>;
}
const App: React.FC = () => {
return <Greeter name="John Doe" />;
}
In this example, Greeter
is a child component that displays a greeting message with a name using the name
prop passed down from the parent component, App
.
Set
is a function used to update a state value in a functional component. When a state is updated, a re-rendering of the components with the new state occurs.
import React, { useState } from 'react';
const MyComponent: React.FC = () => {
const [count, setCount] = useState<number>(0);
return (
<div>
<h1>Count: {count}</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
In this example, the setCount
function is called when the Increment
button is clicked. The value of count is updated by the setting count + 1
.
A rendercycle
is the process by which React selects which components to render on a given screen and updates them accordingly based on their current state. React components usually mount, update, and unmount over their lifetime, which is called a "lifecycle" of a component.
import React, { useState } from 'react';
const MyComponent: React.FC = () => {
const [count, setCount] = useState<number>(0);
return (
<div>
<h1>Count: {count}</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
In this example, initially MyComponent
will mount with count
being 0
. Once the user clicks the Increment
button, setCount
is called, and count is updated to 1. This will trigger a re-render cycle in which the component is re-rendered with the new state value.
In summary, Typescript React's useState
Hook is a powerful tool that allows developers to add state functionality to functional components and control the state of an application dynamically. Understanding State
, component
, props
, set
, and rendercycle
is crucial to building robust and scalable web applications using Typescript React. By utilizing these tools, developers can create complex web applications, that are simple to reason about and debug.