Wednesday, 6 September 2017

Explain state and props in React

1. state=> The heart of every React component is its “state”, an object that determines how that component renders & behaves. In other words, “state” is what allows you to create components that are dynamic and interactive.
state is mutable
2.props=> props is immutable and can be used to communicate between child and parent component.

Explain setState() and forceUpdate() ?

1.Whenever we call setState() method React Component get re-render.
2. State object is used to mentain Component level data which value we want to change on some action and purpose is to update DOM without page refresh.
3. Whenever setState() method get call it also invoke the lifecycle methods (Not All, e.g. if it get called from componentWillMount() state get updated but component will render only once).
4. setState is asynchronous method and also has optional parameter as callback function
   e.g=>
        this.setState({'Name':'Mayur'},function(){
              //here you will get updated state
        });
5. We can pass a function to setState that receives the previous state and props and returns a new state.
     e.g=>
        this.setState((prevState, props) => {
          return {
             amount: prevState.patient.amount - props.patient.concession
          }
        });
6.  In Some scenario we use data using global variable that means render() method also having data other than this.state and this.props. values in this case forceUpdate() method can be used to update component values.

What is diffing algorithm?

  React needs to use algorithms to find out how to efficiently update the UI to match the most recent tree. The diffing algorithms is genera...