JS Guide
HomeQuestionsSearchResources
Search

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext
react
junior
props

What are props in React and how do you pass data between components?

props
data-flow
components
children
callbacks
Quick Answer

Props (properties) are read-only inputs passed from parent to child components, similar to function arguments. They enable component reusability and one-way data flow from parent to child.

Detailed Explanation

Props Basics:

  • Short for 'properties'
  • Passed from parent to child component
  • Read-only (immutable) within the child
  • Can be any JavaScript value (strings, numbers, objects, functions)

Key Concepts:

  • One-way data flow: Data flows down from parent to child
  • Props vs State: Props come from outside, state is internal
  • Default props: Provide fallback values
  • Destructuring: Common pattern to extract props
  • children prop: Special prop for nested content

Data Flow Patterns:

  • Parent → Child: Pass via props
  • Child → Parent: Pass callback functions as props
  • Siblings: Lift state to common parent

Code Examples

Basic props usage
// Parent component
function App() {
  const user = { name: 'Alice', age: 25 };
  
  return (
    <div>
      {/* Passing different prop types */}
      <Greeting 
        name="Alice"           // string
        age={25}               // number
        isLoggedIn={true}      // boolean
        user={user}            // object
        hobbies={['reading']}  // array
        onClick={() => {}}     // function
      />
    </div>
  );
}

// Child component with destructuring
function Greeting({ name, age, isLoggedIn }) {
  return (
    <div>
      <h1>Hello, {name}!</h1>
      <p>Age: {age}</p>
      {isLoggedIn && <p>Welcome back!</p>}
    </div>
  );
}

// With default props
function Greeting({ name = 'Guest', age = 0 }) {
  return <p>{name} is {age} years old</p>;
}

Resources

React Docs - Passing Props to a Component

docs

React Docs - Sharing State Between Components

docs

Related Questions

What is state in React and how is it different from props?

junior
state
Previous
What is the difference between functional and class components in React?
Next
What is state in React and how is it different from props?