/
githubmirror
/
solid
Обзор
Документация
Войти
/
githubmirror
/
solid
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/solid-ssr/examples/shared/src/router.js
39 строк
1 KB
花果山大圣
add prettier code format in git-commit-hook (#1769)
27 июн 2023, 02:07
Не верифицирован
27 июн 2023, 02:07
e660e5a
Код
Авторство
О чём код?
import { createSignal, createContext, useContext, useTransition } from "solid-js"; import { isServer } from "solid-js/web"; // Super simplistic pushstate router that matches on absolute paths const RouterContext = createContext(); function RouteHOC(Comp) { return (props = {}) => { const [location, setLocation] = createSignal( (props.url ? props.url : window.location.pathname).slice(1) || "index" ), matches = match => match === (location() || "index"), [pending, start] = useTransition(); !isServer && (window.onpopstate = () => setLocation(window.location.pathname.slice(1))); return ( <RouterContext.Provider value={[location, pending, { setLocation: v => start(() => setLocation(v)), matches }]} > <Comp /> </RouterContext.Provider> ); }; } const Link = props => { const [, , { setLocation }] = useContext(RouterContext); const navigate = e => { if (e) e.preventDefault(); window.history.pushState("", "", `/${props.path}`); setLocation(props.path); }; return ( <a class="link" href={`/${props.path}`} onClick={navigate}> {props.children} </a> ); }; export { RouteHOC, RouterContext, Link };