I am building a website using react
and react-router-dom
and I encountered a problem.
When I have a footer element that is being rendered on every web page like so:
<Router>
<Header />
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="shop/*" element={<ShopRoutes />} />
<Route path="cart" element={<CartPage />} />
<Route path="login" element={<LoginPage />} />
<Route path="register" element={<RegisterPage />} />
</Routes>
<Footer />
</Router>
On pages that you need to scroll down for the footer to be displayed at the bottom of the page, you need to add position: relative;
.
And on pages without the need to scroll down the footer to be displayed at the button you need to add position: absolute;
.
How do I make it that on every page the style is different depending on the page length?
I can set a useState
in the app.jsx
file that will do a conditional class naming to the footer element, and on every page, I will set the state to be true or false:
app.jsx:
const [pageBig, setPageBig] = useState(false);
<Router>
<Routes>
<Route path="/" element={<HomePage setPageSize={setPageBig}/>} />
..... more routs
</Routes>
<Footer pageSize={pageBig}/>
</Router>
footer.jsx:
<footer className={ props.pageSize ? "big-page" : "small-page"}>
<p>© Copyright {currentYear}</p>
</footer>
for example, if the homePage is a long page I will set the pageBig
to true and the footer class will change:
props.setPageSize(true);
I can also do the same with createContex
but in both solutions, I will need to make sure in every page I set the bigPage
to be the correct one.
How can I achieve this functonltiy?