Friday, September 11, 2020

Accessing props

 Let's understand how to access props through class component.

import React from "react";


// Class Component
class Basic extends React.Component {
  // constructor is used for initialization and writing super
// will call the
 //constructor of the class react.component[parent class]
  //this refer to the current object of the class
  constructor(props) {
    //this keyword is coming from super by react.component 
//therefore you will 
//get error when run it
    // state also work like props to assign value
    super(props);
    //accessing props in class component using this keyword
    console.log(this.props);
    this.state = { name: "computer"price: "200" };
  }
  render() {
    return (
      <div>
        <p>hello</p>
        <h1>
          {/* {this.state.Productname} {this.state.price} */}
          {this.props.productname}
          {this.props.price}
          {this.state.name}

          {/* {this.props} */}
        </h1>
      </div>
    );
  }
}
export default Basic;
//ReactDom.render(<Car />, document.querySelector("#root"));

No comments:

Post a Comment