Ojasvi Monga
Ojasvi Monga

Reputation: 5119

What happens to the state of a react component when the component is re-rendered?

What happens to the state of a react component when the component is re-rendered?

Does it remain unaffected, or does it get reset to some 'default' value? Why?

Upvotes: 1

Views: 394

Answers (3)

Pavan Kalyan
Pavan Kalyan

Reputation: 407

Change in the state will re-render with an updated state.

A state can be changed either by user interaction or a life cycle method only.

When a state is changed, only the updated part will be rendered

Upvotes: 0

Anku Singh
Anku Singh

Reputation: 954

When you refresh the page, components get re-rendered and state get's reset to default value is it because when component re-rendered we have following lifecycle of react that runs

  1. Initialization here react constructor() run and set the state to default value

  2. Mounting

    componentWillMount() , componentDidMount()

  3. Updation

    componentWillRecieveProps(), shouldComponentUpdate(), componentWillUpdate(),componentDidUpdate()

  4. Unmounting - This run when you are leaving the component

    componentWillUnmount()

Upvotes: 1

Ojasvi Monga
Ojasvi Monga

Reputation: 5119

  • The state does not depend on the rendering of a component and remains unaffected by re-rendering.
  • The state gets updated only by a lifecycle method or by a call to setState.

Upvotes: 0

Related Questions