Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.babelrc
.editorconfig
.gitignore
README.md
index.js
package-lock.json
package.json
webpack.config.js

README.md

react-ssr-use-cookie

Example of working with cookie in universe react applications. Read more «Работа с куками в универсальных приложениях на react» (Article is written on russian yet).

// src/client.jsx

import { CookiesProvider } from './services/cookie';

const jsx = (
    <CookiesProvider>
        <App />
    </CookiesProvider>
);

hydrate(jsx, document.getElementById('mount'));
// src/server.jsx

import { CookiesProvider, ServerCookiesManager } from './services/cookie';

const app = express();

app
    .use(cookieParser())
    .use(render);

function render(req, res) {
    const cookieManager = new ServerCookiesManager(req, res);

    const jsx = (
        <CookiesProvider manager={cookieManager}>
            <App />
        </CookiesProvider>
    );
    const reactDom = renderToString(jsx);

    res.send(htmlTemplate(reactDom));
}

Component:

export default function CookiesNotification() {
    const [cookie, setCookie] = useCookies(COOKIE_KEY);
    
    if (cookie) {
        return null;
    }

    return	(
        <button	onClick={() => setCookie(true)}>
            Accept use cookie
        </button>
    );
}
You can’t perform that action at this time.