I am seeking a way to have my React component smoothly fade in upon being mounted. The component's outermost DIV starts with inline style display:none
. Within the componentDidMount()
method, I've written the following code:
let el = document.querySelector('.myElement');
el.style.transition="opacity 2s";
el.style.opacity=1;
Despite these efforts, the components remain hidden when rendered. Adding el.style.display='block';
results in an instant appearance without the desired fade-in effect.
I experimented with jQuery's fadeIn
method and achieved the intended outcome:
$(el).fadeIn(2000);
This prompts the question: is it feasible to achieve this using pure JavaScript?
Note: This specific component is utilized in two scenarios - when a new component is added to the page, and when an existing component loads data from the database on page load. I only aim to have the fade-in transition occur when a new component is included on the page.