Перебросил useEffect из Main в Root и оформил как контекст

This commit is contained in:
Ifraim Solomonov committed 2024-03-04 11:38:08 +03:00
1 parent 8496cae1aa
commit ce0752c7eb
5 files changed
+72 -55

No files matched your search

+24 -3
View File
@@ -1,25 +1,46 @@
import { Outlet, useLoaderData } from "react-router-dom";
import { createContext, useEffect } from 'react';
import { getContacts } from "../../api";
import useElementOnScreen from '../../lib/useElementOnScreen';
import Navbar from '../../components/Navbar';
import Footer from "../../components/Footer";
import './Root.styles.scss';
export const RootContext = createContext();
async function loader() {
const contacts = await getContacts();
return { contacts };
}
function Root() {
const { contacts } = useLoaderData();
const [containerRef, isVisible] = useElementOnScreen({
root: null,
rootMargin: '0px',
threshold: 0,
});
useEffect(() => {
const [navbar] = document.getElementsByClassName('navbar');
if (isVisible) {
navbar.classList.add('absolute');
} else {
navbar.classList.remove('absolute');
}
}, [isVisible]);
return (
<>
<RootContext.Provider
value={{ containerRef }}
>
<Navbar />
<div id="page">
<Outlet />
</div>
<Footer />
</>
</RootContext.Provider>
);
}