For React JS
I need to show a text in
after 2 seconds, it’s work in console but not in the DOM
const items = document.getElementById("items");
const errorDisplay = () => {
setTimeout(function () {
items.innerHTML = "please wait..";
console.log(items);
}, 2000);
};
<p id="items"> </p>
Thanks
const items = document.getElementById("items");
const errorDisplay = () => {
setTimeout(function () {
items.innerHTML = "please wait..";
console.log(items);
}, 2000);
};
<p id="items"> </p>
3
Answers
Minimal example of React app with
setState
for controlling the visibility of the wait text, anduseEffect
to use thesetTimeout
function:HTML:
JavaScript:
See https://codepen.io/michaeljoh/pen/RweVBpJ
If you want to show a please wait paragraph for two seconds before displaying the products you can do something like this:
Or refactor the returning jsx to this depending on your readability preferences: