Skip to content
Advertisement

How can update the DOM if the state changes?

I’ve created the box container dynamically according to the input changes.

  1. If I entered 1, it will create one box
  2. If I change the input lets say 2, its create 3 box but it should create 2
JavaScript
JavaScript

Do I need to cleanup the dom. if so how to do it?. or is there any better way to implement the same.

Please help. ty:)

Advertisement

Answer

You should avoid doing direct manipulations on the DOM. Instead create a “Box” react component and render it based on the amount of your value state.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement