State Propagation in REACT - Js Using UseState Hook
State Propagation in REACT - Js Using UseState Hook
In REACT.JS
Using useState() Hook
Abdulrazak Attar
Author SDE @
INDEX
1. What is State Propagation
2. Why React.js came into Picture
3. UI update when State changes in classic JavaScript
4. UI update when State changes in React.js
5. Summary
<div id="displayArea1"></div>
<div id="displayArea2"></div>
<div id="displayArea3"></div>
<div id="displayArea4"></div>
<div id="displayArea5"></div>
<button>Update Variable</button>
</body>
<script>
btn.addEventListener("click", () => {
variableValue = 'updated value'
updateUI();
});
</script>
Soo, as we can see here, the code has became clumsy. Even though we don’t
access and store the elements in variables (as I did above element1,2,etc) and
access them directly in functions itself, then also it is tedious and for more
complex apps, it will become even more clumsy.
So here, in short, to UPDATE the UI based on State, we need to write more code
manually.
BUT, this UPDATE thing we can do it very easily by REACT. In fact we no need to
update the UI, React itself will do it for us. Lets see the same scenario with React
now....!
function App() {
let [defaultValue, setDefaultValue] = useState("default value");
return (
<>
<div>{defaultValue}</div> need
<div>{defaultValue}</div> ly we
c h on
<div>{defaultValue}</div>
is mu
<div>{defaultValue}</div> it. Th ite
t’s wr
<div>{defaultValue}</div> h, tha to
a
//Ye
<button onClick={updateUI}>Update UI</button>
</>
);
}