Tarjima qoshish uchun havola boyicha o'tib Pull Request oching (havolaga o'tish).
Standart til uchun tarkibni ko'rsatadi.
There is the official Next.js bindings package - @effector/next
. Follow its documentation to find out, how to integrate Next.js with effector.
This is a simplified example of integration with the Next.js router. We create a similar model for storing the router instance:
import { attach, createEvent, createStore, sample } from 'effector'
import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime'
const routerAttached = createEvent<AppRouterInstance>()
const navigationTriggered = createEvent<string>()
const $router = createStore<AppRouterInstance | null>(null).on(
routerAttached,
(_, router) => router,
)
const navigateFx = attach({
source: $router,
effect: (router, path) => {
if (!router) return
return router.push(path)
},
})
sample({
clock: navigationTriggered,
target: navigateFx,
})
export { navigationTriggered, routerAttached }
We make provider:
import { useUnit } from 'effector-react';
import { useRouter } from 'next/navigation'
export function EffectorRouterProvider({ children }: { children: React.ReactNode }) {
const router = useRouter()
const attachRouter = useUnit(routerAttached)
useEffect(() => {
attachRouter(router)
}, [router, attachRouter])
return <>{children}</>
}
We use provider:
import { EffectorRouterProvider } from '@/providers/effector-router-provider';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<EffectorRouterProvider>
{children}
</EffectorRouterProvider>
</body>
</html>
);
}
And we use it in our models:
import { sample } from 'effector';
...
sample({
clock: getUserFx.done,
fn: () => '/home',
target: navigationTriggered,
});
or in components:
'use client';
import { useUnit } from 'effector-react';
import { navigationTriggered } from '@/your-path-name';
...
export function goToSomeRouteNameButton() {
const goToSomeRouteName = useUnit(navigationTriggered);
return (
<button onClick={() => goToSomeRouteName('/some-route-name')}>
do it!
</button>
);
}
Ingliz tilidagi hujjatlar eng dolzarb hisoblanadi, chunki u effector guruhi tomonidan yozilgan va yangilanadi. Hujjatlarni boshqa tillarga tarjima qilish jamiyat tomonidan kuch va istaklar mavjud bo'lganda amalga oshiriladi.
Esda tutingki, tarjima qilingan maqolalar yangilanmasligi mumkin, shuning uchun eng aniq va dolzarb ma'lumot uchun hujjatlarning asl inglizcha versiyasidan foydalanishni tavsiya etamiz.