gatsby-window

1.0.5 • Public • Published

Example

$ npm install gatsby-window --save

or

$ yarn add gatsby-window

Import usage

import { windowGatsby } from "gatsby-window" 
// ES2015 modules
const { windowGatsby } = require("gatsby-window");

Use example

import React, { Component } from "react";
import { windowGatsby as windowG } from "gatsby-window"

export default class GatsbyPage extends Component {

    componentDidMount() {
        // Using gatsby-window in only-browser methods
        const { sessionStorage } = windowG
        sessionStorage.setItem("componentDidMount", "Using session storage in Gatsby after build!");
        console.log(sessionStorage.getItem("componentDidMount"));
    };

    componentWillUnmount() {
        // Using gatsby-window in only-browser methods
        windowG.sessionStorage.setItem("componentWillUnmount", "Using session storage in Gatsby after build!");
        console.log(windowG.sessionStorage.getItem("componentWillUnmount"));
    };

    render() {
        // Using gatsby-window in other methods
        if (windowG) {
            const { sessionStorage } = windowG
            sessionStorage.setItem("key", "Using session storage in Gatsby after build!");
            console.log(sessionStorage.getItem("key"));
        }

        return (
            <h1>A</h1>
        )
    }
}

Package Sidebar

Install

npm i gatsby-window

Weekly Downloads

26

Version

1.0.5

License

ISC

Unpacked Size

2.74 kB

Total Files

4

Last publish

Collaborators

  • luispa