Redirect to any URL in NextJS both on the client and on the server depending on the state of the store
npm install effector-next-redirect
or yarn
yarn add effector-next-redirect
-
Redirect to the
/login
page, depending on the state of the$isAccessDenied
store// pages/index.jsx import React from "react"; import { withRedirect } from "effector-next-redirect"; import { createStore } from "effector"; export const $isAccessDenied = createStore(true); // redirect with setting 302 status code const enhance = withRedirect("/login", $isAccessDenied); export default function HomePage() { return ( <div> <h1>HomePage</h1> </div> ); } export default enhance(HomePage);
-
Redirect to the
/login
page using replace on the client// pages/index.jsx import React from "react"; import { withRedirect } from "effector-next-redirect"; import { createStore } from "effector"; export const $isAccessDenied = createStore(true); // redirect with setting 301 status code const enhance = withRedirect("/login", $isAccessDenied, { code: 301, replace: true }); export default function HomePage() { return ( <div> <h1>HomePage</h1> </div> ); } export default enhance(HomePage);
The withRedirect
function expects to receive the settings object as the third argument:
asUrl
(string, optional) : maskurl
for the browsercode
(number, optional) : status code set by the server after redirectionreplace
(boolean, optional) : useRouter.replace
instead ofRouter.push
in browser