REACT PROPS

 PROPS  ARE THE INFORMATION THAT YOU PASS TO A JSX TAG.

JUST LIKE AS ARGUMENTS ARE SENT ALONG WITH THE FUNCTIONS , PROPS ARE SENT TO THE COMPONENTS.

THEY ARE SENT IN THE FORM OF ATTRIBUTES(SIMILAR TO HTML)

EVEN THOUGH WE CAN USE props.title TO ACCESS PROPS. WE DONOT USE THEM IN REACT.

WE DE-STRUCTURE THE PROPS, AND SEND THE REQUIRED ELEMENTS IN {}.

NUMBERS ARE SENT DIRECTLY IN {}.

PRODUCT.JSX

import "./Product.css";
function Product({title,price}) {
  return (
    <div className="Product">
      <h4>{title}</h4>
      <h5>price:{price}</h5>
    </div>
  );
}
export default Product;

PRODUCTTAB.JSX

import Product from "./Product";

function ProductTab({title,price}) {
  return (
    <>
      <Product title="phone" price={30000} />
      <Product title="laptop" price={50000} />
      <Product title="pen" price={30}/>
    </>
  );
}
export default ProductTab;




 DEFAULT PARAMETERS CAN BE SENT , WHENEVER A VALUE IS NOT DEFINED , THE DEFAULT VALUE IS USED.

PRODUCT.JSX

import "./Product.css";
function Product({ title, price=7 }) {
  return (
    <div className="Product">
      <h4>{title}</h4>
      <h5>price:{price}</h5>
    </div>
  );
}
export default Product;

PRODUCTTAB.JSX

import Product from "./Product";

function ProductTab({title,price}) {
  return (
    <>
      <Product title="phone" price={30000} />
      <Product title="laptop" price={50000} />
      <Product title="pen"/>
    </>
  );
}
export default ProductTab;




Comments

Popular posts from this blog

DEPENDENCIES IN useEffect()

ACTIVITY1

CONDITIONALS