Props

  • 1. Step
    Props are arguments passed into React components.

    1. Pass Data

    
    
                  function Car(props) {
                    return <h2>I am a { props.brand }!</h2>;
                  }
    
                  function Garage() {
                    return (
                      <>
                        <h1>Who lives in my garage?</h1>
                        <Car brand="Ford" />
                      </>
                    );
                  }
    
                  

    2. send variable

    
    
                  function Car(props) {
                    return <h2>I am a { props.brand }!</h2>;
                  }
    
                  function Garage() {
                    const carName = "Ford";
                    return (
                      <>
                        <h1>Who lives in my garage?</h1>
                        <Car brand={ carName } />
                      </>
                    );
                  }
    
                  

    3. send object

    
    
                  function Car(props) {
                    return <h2>I am a { props.brand.model }!</h2>;
                  }
    
                  function Garage() {
                    const carInfo = { name: "Ford", model: "Mustang" };
                    return (
                      <>
                        <h1>Who lives in my garage?</h1>
                        <Car brand={ carInfo } />
                      </>
                    );
                  }