mirror of
https://git.rdcenter.ru/RDCenter/skb-kit.git
synced 2026-12-05 05:49:45 +00:00
Перебросил useEffect из Main в Root и оформил как контекст
This commit is contained in:
1 parent
8496cae1aa
commit
ce0752c7eb
5 files changed
+72
-55
No files matched your search
+24
-3
@@ -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>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user