I'm using React, react-router and materialize css. I want to add a simple parallax effect on my homepage and it is working when I fist load the page:
import React from 'react'
import M from 'materialize-css';
import Cinema from '../cinema1.jpg'
const Home = () =>{
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.parallax');
M.Parallax.init(elems);
});
return(
<section>
<div id="index-banner" className="parallax-container">
<div className="section no-pad-bot">
<div className="container">
<h1 className="header center">Material </h1>
<div className="row center">
<h5 className="header col s12 white-text">A parallax landing example</h5>
</div>
<div className="row center">
<a href="http://materializecss.com/getting-started.html" id="download-button" className="btn-large waves-effect waves-light teal lighten-1">Get Started</a>
</div>
</div>
</div>
<div className="parallax"><img src={ Cinema } alt="Unsplashed background img 1" /></div>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi et laoreet felis. Sed rhoncus vestibulum erat ut luctus. Aenean nec sem sagittis, pulvinar elit at, bibendum enim. Vivamus blandit, diam vel venenatis vehicula, dolor neque feugiat erat, id mollis metus mi eu leo. Duis suscipit vestibulum tortor. Quisque eget urna non est tincidunt gravida. Aenean sed ex mi. Fusce aliquet mollis finibus. Nam tristique pulvinar orci, cursus pulvinar lectus blandit id. Cras nulla est, viverra eget ultrices sit amet, vulputate non felis. Mauris condimentum dapibus tempor. Etiam sed leo metus. Mauris convallis nunc eu porttitor interdum. </p>
</div>
</section>
)
}
When I change the route and go back to the homepage the parallax is not initiated again. How come that "M.Parallax.init(elems);" stops working when the route changes? Should I write a function to fire it up everytime I click on the home route? What I'd like to know is why it stops working most of all.