diff --git a/gatsby-browser.js b/gatsby-browser.js index 8e155d4..cb8dd92 100644 --- a/gatsby-browser.js +++ b/gatsby-browser.js @@ -1,3 +1,5 @@ +import React from 'react' +import AppProvider from './src/store/provider' import wrapPageElementWithTransition from './src/helpers/wrapPageElement' import './src/styles/global.scss' @@ -6,5 +8,11 @@ if (typeof window !== 'undefined' && !window.IntersectionObserver) { import('intersection-observer') } +// React Context in Browser +// eslint-disable-next-line +export const wrapRootElement = ({ element }) => { + return {element} +} + // Page Transitions & Layout export const wrapPageElement = wrapPageElementWithTransition diff --git a/gatsby-ssr.js b/gatsby-ssr.js index ba55016..1f8c9ea 100644 --- a/gatsby-ssr.js +++ b/gatsby-ssr.js @@ -1,4 +1,13 @@ +import React from 'react' +import { renderToString } from 'react-dom/server' +import AppProvider from './src/store/provider' import wrapPageElementWithTransition from './src/helpers/wrapPageElement' +export const replaceRenderer = ({ bodyComponent, replaceBodyHTMLString }) => { + // React Context in SSR/build + const ConnectedBody = () => {bodyComponent} + replaceBodyHTMLString(renderToString()) +} + // Page Transitions & Layout export const wrapPageElement = wrapPageElementWithTransition diff --git a/src/store/createContext.jsx b/src/store/createContext.jsx new file mode 100644 index 0000000..25bffa4 --- /dev/null +++ b/src/store/createContext.jsx @@ -0,0 +1,5 @@ +import { createContext } from 'react' + +const { Provider, Consumer } = createContext() + +export { Provider, Consumer } diff --git a/src/store/provider.jsx b/src/store/provider.jsx new file mode 100644 index 0000000..88defb0 --- /dev/null +++ b/src/store/provider.jsx @@ -0,0 +1,17 @@ +import React, { Component } from 'react' +import PropTypes from 'prop-types' +import { Provider } from './createContext' + +export default class AppProvider extends Component { + state = { + dark: false + } + + static propTypes = { + children: PropTypes.any.isRequired + } + + render() { + return {this.props.children} + } +}