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

What are props in React Native?

Câu trả lời

Props in React Native, short for properties, are a fundamental concept used to pass data from one component to another. They allow components to be customized and reused with different data. Here’s a detailed explanation:

Definition and Usage

Props are immutable data that are passed from a parent component to a child component. They are used to configure a component and control its behavior or appearance. Unlike state, which is managed within a component, props are managed outside the component and passed down the component tree.

How Props Work

When a parent component renders a child component, it can pass data to the child component through props. The child component receives these props as an argument in its function or as a property in its class.

For example, consider a simple Greeting component that takes a name prop:

javascript Copy
// Parent Component
export default function App() {
  return (
    <View>
      <Greeting name="John" />
      <Greeting name="Jane" />
    </View>
  );
}

// Child Component
function Greeting(props) {
  return <Text>Hello, {props.name}!</Text>;
}

In this example, the App component passes the name prop to the Greeting component, which then uses it to display a personalized greeting.

Key Characteristics

  1. Immutable: Props are read-only and cannot be modified by the receiving component. This ensures that data flows in a single direction, from parent to child, making the application easier to understand and debug.
  2. Reusable: By using props, you can create reusable components that can be configured differently in various parts of your application.
  3. Customizable: Props allow you to customize the behavior and appearance of components. For instance, you can pass different styles, event handlers, or data to a component via props.

Example of Using Props

Here’s a more detailed...

junior

junior

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

junior

What are the advantages of native apps over hybrid apps?

expert

How is InteractionManager important?

senior

What are features of presentational/dumb components?

Bình luận

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

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