Câu hỏi phỏng vấn React Native
Câu hỏi

What are Containe...

Câu trả lời

Container components, also known as smart components, are a fundamental concept in React and React Native development. These components are primarily responsible for handling the application's data and logic, rather than focusing on the presentation or UI. Here are the key characteristics and roles of container/smart components:

Key Characteristics of Container/Smart Components

  1. State Management:

    • Container components manage the state of the application. They often use hooks like useState and useEffect in functional components or maintain state within class components using the constructor and lifecycle methods[1][2][5].
  2. Data Handling:

    • These components are responsible for fetching, manipulating, and storing data. They may interact with APIs, external libraries, or Redux to manage the application's data flow[1][2][5].
  3. Logic Implementation:

    • Container components contain the business logic of the application. They handle user interactions, side effects, and other complex operations that are necessary for the app's functionality[1][2][5].
  4. Passing Data to Presentational Components:

    • They pass data and callback functions to presentational (or dumb) components via props. This separation allows presentational components to focus solely on rendering the UI based on the data they receive[1][2][5].
  5. Minimal Styling:

    • Container components typically do not include extensive styling. Their primary focus is on functionality, leaving the styling to presentational components[1][2][5].

Example of a Container/Smart Component

Here is a simple example of a container component in React:

javascript Copy
import React, { useState, useEffect } from "react";
import axios from "axios";
import WelcomeMessage from "./WelcomeMessage";

function WelcomeContainer() {
  const [name, setName] = useState("");

  useEffect(() => {
    axios.get("/api/user").then((response) => setName(response.data.name));
  }, []);

  return <WelcomeMessage name={name} />;
}

export default WelcomeContainer;

In this example:

  • WelcomeContainer is a container component that fetches user data from an API.
  • It manages the state (name) and passes this state as a prop to the WelcomeMessage component, which is a presentational component responsible for displaying the data.

Benefits of Using Container/Smart Components

  1. Separation of Concerns:

    • By separating the logic and data handling from the UI, the code becomes more modular, easier to maintain, and scalable[1][2][5].
  2. **...

senior

senior

Gợi ý câu hỏi phỏng vấn

junior

What are the types of data that control a component?

middle

In Fast Refresh, what will happen if you edit files imported by modules outside of the React Tree?

middle

What are some best practices to consider for an action?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào