1.0.1 • Published 6 years ago
effector-next-redirect v1.0.1
Effector Next Redirect
Redirect to any URL in NextJS both on the client and on the server depending on the state of the store
Installation
npm install effector-next-redirector yarn
yarn add effector-next-redirectExample
Redirect to the
/loginpage, depending on the state of the$isAccessDeniedstore// 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
/loginpage 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);
Configuration
The withRedirect function expects to receive the settings object as the third argument:
asUrl(string, optional) : maskurlfor the browsercode(number, optional) : status code set by the server after redirectionreplace(boolean, optional) : useRouter.replaceinstead ofRouter.pushin browser