0

How React knows about props, i am sending props to component and i am not accepting arguments on constructor but render() method access props.

import ReactDOM from 'react-dom';
import './index.css';

import App from './App';

import reportWebVitals from './reportWebVitals';
ReactDOM.render(
  <React.StrictMode>
    <App myApp={'myApp'}/>
  </React.StrictMode>,
  document.getElementById('root')
);
// this is app component
import React, {Component} from 'react';
class App extends Component {
  constructor() {
    super()
    this.state = {};
    console.log(this.props.myApp) // undefined
  }
  render() {
    console.log(this.props.myApp) // 'myApp' how and why, i'm not accepting arguments in contrutor why and how render method know this.props.myApp
  }
}
exports default App;
// this is my assumption what react doing
const obj = new App(props);

1 Answers1

0

You need to rebuild your constructor:

constructor(props) {
  super(props);
  console.log(this.props.myApp); // 'myApp'
}

More info here: What does calling super() in a React constructor do?

  • I want to know how render() method access props when i am not accepting arguments in constructor – javascript lover Nov 24 '20 at 21:44
  • Similar discussion can be find here: https://stackoverflow.com/questions/30571875/whats-the-difference-between-super-and-superprops-in-react-when-using-e "React sets .props on the instance from the outside immediately after calling the constructor." – Banan3k Nov 24 '20 at 21:49