How do you pass a prop to a component in JSX?

Master ReactJS with our comprehensive test. Practice with multiple-choice questions and detailed explanations. Build your skills for the exam with our engaging format and expert tips!

Multiple Choice

How do you pass a prop to a component in JSX?

Explanation:
Props are passed to a component in JSX by adding attributes to the component element. Those attributes become the props that the component receives. For example, rendering <Greeting name="Alice" /> passes a prop named name with the value "Alice" to Greeting, and inside Greeting you can access it as props.name (or by destructuring to { name }). You can pass any valid expression, like <Greeting name={userName} /> or <Greeting active={isActive} />. Props are read-only from the child’s perspective; if you need to change data, you typically use state or callbacks passed down from the parent. Mutating the component’s state is about internal data, not how you pass data to a child. Calling a prop as a function inside render is a pattern for using function props, not the act of passing a prop itself. Using a global variable could be used in the value, but the standard mechanism is still through attributes on the JSX element.

Props are passed to a component in JSX by adding attributes to the component element. Those attributes become the props that the component receives. For example, rendering passes a prop named name with the value "Alice" to Greeting, and inside Greeting you can access it as props.name (or by destructuring to { name }). You can pass any valid expression, like or . Props are read-only from the child’s perspective; if you need to change data, you typically use state or callbacks passed down from the parent. Mutating the component’s state is about internal data, not how you pass data to a child. Calling a prop as a function inside render is a pattern for using function props, not the act of passing a prop itself. Using a global variable could be used in the value, but the standard mechanism is still through attributes on the JSX element.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy