question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to initialize redux store with data from Gatsby page query?

See original GitHub issue

Summary

How to initialize redux store with data from a Gatsby page query?

Is there a way to use the wrapRootElement function on ssr?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:25 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
Js-Brechtcommented, Apr 17, 2020

This is how I handle that in one of the projects I’m working on:

import React from 'react';
import { Provider } from 'react-redux';
import { StaticQuery, graphql, GatsbyBrowser } from 'gatsby';
import { createStore } from '~/store/store';
import { actions } from '~/store';
import { MenuDataQuery } from '@/graphql/types';

const { hydrateMenuState } = actions.ui.menu;

export const wrapRootElement: GatsbyBrowser['wrapRootElement'] = ({ element }): React.ReactNode => {
    const store = createStore();
    const dispatch = store.dispatch;
    return (
        <StaticQuery
            query={graphql`
                query MenuData {
                    allMenuItem {
                        edges {
                            node {
                                id
                                url
                                slug
                                label
                                acl
                                routes
                                children {
                                    id
                                }
                            }
                        }
                    }
                }
            `}
            render={(data: MenuDataQuery) => {
                dispatch(hydrateMenuState(data));
                return <Provider store={store}>{element}</Provider>;
            }}
        />
    );
};

It doesn’t really do a lot at the moment, but the concept is there.

It’s also possible to use hooks if you wrap it in another React component

0reactions
yidongwcommented, May 4, 2021

@Js-Brecht I’m using mobx-state-tree for storing data, and it contains some product data that doesn’t change very often, so I want to get those data ready in the build time. Fetching those data is written in mobx-state-tree action, and it is REST API instead of graphQL.

I implemented wrapRootElement in gatsby-ssr.js and gatsby-browser.js. wrapRootElement is the same function that creates a store and wrapping the element with context provider. According to some tutorial.

import { MainContext, MainModel } from "../stores/MainStore"

export default ({ element }) => {
  const store = MainModel.createStore({})
  return <MainContext.Provider value={store}>{element}</MainContext.Provider>
}

I also need to createPage in gatsby-node.js according to using-gatsby-without-graphql, but all the data fetching is currently done in the store, so in order to fetch the data, I need to somehow access the store that was created in the gatsby-ssr.js or gatsby-browser.js.

How am I supposed to do that? Or I should use a different approach?

Your answer is the closest approach, but you are using graphQL, so I don’t know what I should do with my stuff.

I also posted my questions here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Gatsby + Redux While Preserving Site Performance
Create a custom Redux store with reducers and initial state, ... Commons, libraries used on every Gatsby page, commons-[hash].js.
Read more >
Gatsby: The ultimate guide with examples - LogRocket Blog
This tutorial covers everything you'll ever need to know about Gatsby, including code demos and comparisons to React and Next.js.
Read more >
Add data to Gatsby's Data layer using sourceNodes
In this post i'm going to demonstrate how to source data from a NASA API and inject the response into Gatsby's GraphQL layer...
Read more >
Renovate Bot Package Diff
utils/page-data\"\nimport {\n structureWebpackErrors,\n ... any>\n): Promise<void> {\n // global gatsby object to use without store\n global.
Read more >
Executing async code on update of state with react-hooks
then((o) => { const { orders: fetchedOrders } = o. ... data; const allOrders = orders.concat(fetchedOrders); setOrders(allOrders); setPage(page + 1); setPagesSeen ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found