What are Containe...
What are Containe...
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:
State Management:
useState
and useEffect
in functional components or maintain state within class components using the constructor
and lifecycle methods[1][2][5].Data Handling:
Logic Implementation:
Passing Data to Presentational Components:
Minimal Styling:
Here is a simple example of a container component in React:
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.name
) and passes this state as a prop to the WelcomeMessage
component, which is a presentational component responsible for displaying the data.Separation of Concerns:
**...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào