ReactJS Cơ bản

Props trong ReactJs

Props trong ReactJs

Props là một tính năng trong React cho phép component cha truyền dữ liệu cho component con.

Bạn có thể hiểu Props như một cách truyền tham số từ component cha đến component con.

Ví dụ:

import React from 'react';

// Stateless component
const WelcomeMessage = (props) => {
  return <h1>Hello, {props.name}!</h1>;
};

// Stateful component
class App extends React.Component {
  render() {
    return (
      <div>
        <WelcomeMessage name="Viet" />
        <WelcomeMessage name="Anh" />
      </div>
    );
  }
}

export default App;

Trong ví dụ trên, component WelcomeMessage là một Stateless Component và nhận một thuộc tính name từ component cha App.

Component App render hai component WelcomeMessage với hai giá trị khác nhau cho thuộc tính name.

Kết quả sẽ là hai dòng "Hello, Viet!" và "Hello, Anh!".


Chia sẻ bài viết:
Bình luận trực tiếp trên web
Bài viết liên quan
State trong ReactJS

State trong ReactJS

State là một tính năng trong React cho phép component quản lý trạng thái của chính nó. Trạng thái là một đối tượng lưu trữ dữ liệu tạm thời của component.

11-02-2023 · ReactJS Cơ bản · 2 phút đọc · 1021
Props Validation trong ReactJS

Props Validation trong ReactJS

Props Validation trong ReactJS là một cách để kiểm tra rằng component con nhận được các props chính xác theo yêu cầu.

11-02-2023 · ReactJS Cơ bản · 3 phút đọc · 994
Các API thường dùng trong React Components

Các API thường dùng trong React Components

Các API thường dùng trong React như render, constructor, componentDidMount, shouldComponentUpdate, componentDidUpdate, componentWillUnmount...

11-02-2023 · ReactJS Cơ bản · 2 phút đọc · 957
Component trong ReactJS

Component trong ReactJS

Trong ReactJS, có hai loại component chính là Stateful Component và Stateless Component.

11-02-2023 · ReactJS Cơ bản · 2 phút đọc · 976