0

I am having a confusion, we say react js has unidirectional flow which means props can be sent from parent to child only. But then why do we write super(props);??

generally we write

class Test extends React.Component { 
    constructor(props) 
    { 
        super(props); 
        this.state = { hello : "World!" }; 
    }
}

so why are we sending props from child to parent?? I expect explanation to my question

John Ruddell
  • 25,283
  • 6
  • 57
  • 86
  • [**`super`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super) has nothing to do with react, but rather object inheritance in javascript. React uses class syntax, your subclasses are extending from `React.Component`. That is a parent react class that sets up its lifecycle so you have methods like `componentDidMount`. This is unrelated to props that one component sends to another – John Ruddell Jul 15 '19 at 17:01
  • Yes, you want to call the parent class constructor so the components lifecycle and other operations are setup. Read the docs on super, i posted a link – John Ruddell Jul 15 '19 at 17:04
  • Possible duplicate of [What does calling super() in a React constructor do?](https://stackoverflow.com/questions/40433463/what-does-calling-super-in-a-react-constructor-do) – John Ruddell Jul 15 '19 at 17:08

1 Answers1

1

But then why do we write super(props);

That has nothing to do with the parent/child relationship of components. That has to do with the superclass/subclass relationship between React.Component and class Test extends React.Component. super(props) calls the React.Component constructor, passing in the props.

Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98