What are props in React?
What are props in React?
In React, "props" is short for "properties." They are a core concept in React that allows data to be passed from one component to another, specifically from parent components to child components. This mechanism is essential for creating dynamic and interactive user interfaces in React applications.
Props are read-only, meaning that a component can only read the props passed to it by its parent component; it cannot modify them. This immutability helps prevent bugs and maintain a predictable flow of data within the application. The data passed as props can include values like strings, numbers, functions, arrays, and even other React components.
Here’s a simple example to illustrate how props work in React:
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
function App() {
return <Welcome name="Alice" />;
}
In this example:
Welcome
component receives props
, an object containing all the properties passed to it.App
component renders the Welcome
component and passes a name
prop to it.Welcome
component accesses the name
prop and uses it within the returned JSX to dynamically generate a greeting message.Props are fundamental to React component communication, allowing developers to create reusable and isolated UI components that can be configured with different data inputs[1][2]...
entry
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào