Danh mụcThẻBài viết

admin

I'm a Full-stack developer

Thẻ

Linked List
Data Structure
Chat GPT
Design Pattern
Microservices
API
AWS CDK
ReactJS
AWS Lightsail
Flutter Mobile
Part 1: React Props and State
Ngày đăng: 18/06/2023

In this article, I will explain what is Props and State in React, and what is different between them. Before learning ReactJS, we should understand Props and State. This is basic for a newbie.


What is State in React?


State is an object that holds some information in a component. The important point is whenever state object is changed, the component is re-rendering.

Let's take an example. We have a DownloadButton component, The label default is Download. Used useState hook to change the label to Processing when the user clicked.


import React, { useState } from 'react';

function DownloadButton() {
  const [btnLabel, setBtnLabel] = useState('Download');

  const download = () => {
    setBtnLabel('Processing');
  }

  return (
    <div>
      <button onClick={download}>{btnLabel}</button>
    </div>
  );
}


What are Props in React?


Props are objects or values that are passed into a component. The main point is to pass data to your component and trigger State changes.

For example, we have 2 components that are ActionSession and DownloadButton. The DownloadButton component is children of ActionSession, it is received a value from the parent which is fileType. Then, the button label is changed after the fileType value is changed.


import React, { useState } from 'react';

interface DownloadButtonProps {
  fileType: 'csv' | 'excel';
}

function DownloadButton({ fileType }: DownloadButtonProps) {
  const [btnLabel, setBtnLabel] = useState('Download');

  const download = () => {
    setBtnLabel('Processing');
  }

  return (
    <div>
      <button onClick={download}>{btnLabel} - {fileType}</button>
    </div>
  );
}

function ActionSession() {
  return <DownloadButton fileType='csv'/>
}


What is different between Props and State?

Both Props and State are used to manage the data of a component but they are used in different ways and have different characteristics.


Props:

  • The data is passed from the parent component.
  • Can be used with State and functional components.
  • Cannot be modified.
  • Read-only.


State:

  • The Data is passed within the component only.
  • Can be modified.
  • Both read and write.


Thank you for reading!


Next articles:
PART 2: THE HOOKS ARE USED POPULARLY IN REACT
PART 3: REACT FRAGMENTS
Đề xuất

RESTful API - How to design?
admin09/06/2023

RESTful API - How to design?
In this article, I will describe the useful points of designing a good API.
TypeScript Design Pattern - Bridge
admin08/08/2023

TypeScript Design Pattern - Bridge
Decouple an abstraction from its implementation so that the two can vary independently.
Form validator in Flutter with EzValidator
admin04/01/2024

Form validator in Flutter with EzValidator
When I am working on Flutter with form. For ensuring data integrity, and handling user input errors. I want to show an error message below each TextField, Dropdown, Switch, ... if the user does not input or wrong input. The EzValidator help me to resolve this.
Mới nhất

Part 4: How to use Redux Toolkit in React
admin18/06/2023

Part 4: How to use Redux Toolkit in React
In this article, I will explain Redux and delve into Redux Toolkit. a collection of tools that simplify using Redux. These tools help make Redux less daunting and easier to use.
Create Cognito User Pool with AWS CDK
admin09/06/2023

Create Cognito User Pool with AWS CDK
In the previous post, I showed you how to create a simple S3 bucket. Next, in this article, I will guide you to create a Cognito User Pool.
TypeScript Design Pattern - Abstract Factory
admin07/08/2023

TypeScript Design Pattern - Abstract Factory
The abstract factory pattern is one of five design patterns in the Creational Design Pattern group. The abstract factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Đinh Thành Công Blog

My website, where I write blogs on a variety of topics and where I have some experiments with new technologies.

hotlinelinkedinskypezalofacebook
DMCA.com Protection Status
Góp ý
Họ & Tên
Số điện thoại
Email
Nội dung
Tải ứng dụng
hotline

copyright © 2023 - AGAPIFA

Privacy
Term
About