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.

Using Easy Peasy in Gatsby

See original GitHub issue

Hello, im getting this error, im using gatsby 2.15.36 with a fresh new install using gatsby new command

Uncaught Error: An error occurred trying to map state in a useStoreState hook: Cannot read property 'getState' of undefined. at useStoreState (easy-peasy.esm.js:41)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
monotykamarycommented, Dec 7, 2019

I have a working setup with TypeScript which works well:

// src/store/models/counter.ts

import { action, Action, thunk, Thunk } from 'easy-peasy';

interface Counter {
  count: number,
  setCount: Action<Counter, number>
  asyncSetCount: Thunk<Counter, number>
}

const counter: Counter = {
  count: 0,

  setCount: action((state, payload) => {
    state.count = payload;
  }),

  asyncSetCount: thunk(async (actions, payload) => {
    await actions.setCount(payload);
  }),
};

export default counter;
// src/store/index.ts

import { createStore, createTypedHooks } from 'easy-peasy';
import counter from './models/counter';

export const models = {
  counter,
};

export const {
  useStore,
  useStoreState,
  useStoreActions,
  useStoreDispatch,
} = createTypedHooks<typeof models>();

export default createStore(models);
// wrapWithProvider.js

import React from 'react';
import PropTypes from 'prop-types';
import { StoreProvider } from 'easy-peasy';
import store from './src/store';

const wrapWithProvider = ({ element }) => (
  <StoreProvider store={store}>
    {element}
  </StoreProvider>
);

wrapWithProvider.propTypes = {
  element: PropTypes.node.isRequired,
};

export default wrapWithProvider;
// gatsby-ssr.js
// gatsby-browser.js

import wrapWithProvider from './wrapWithProvider';

export const wrapRootElement = wrapWithProvider;
2reactions
ctrlplusbcommented, Oct 15, 2019

Hey @wrtisans

I have yet to use Gatsby myself, but in terms of configuring the StoreProvider you would need to follow a similar strategy as if you were implementing a standard react-redux Provider. The Gatsby repo has an example you can reference:

https://github.com/gatsbyjs/gatsby/tree/master/examples/using-redux

Read more comments on GitHub >

github_iconTop Results From Across the Web

easy-peasy examples - CodeSandbox
Learn how to use easy-peasy by viewing and forking easy-peasy example apps on CodeSandbox.
Read more >
Why you should be using Gatsby - JavaScript in Plain English
easy -peasy, graph and squeezy. Instead of Redux, Gatsby uses GraphQL for state management which is increasingly being used with Node.js and ...
Read more >
Easy Peasy v5
Easy Peasy is an abstraction of Redux, providing a reimagined API that focuses on developer experience. It allows you to quickly and easily ......
Read more >
Tailwind CSS and Gatsby in Under 5 Minutes | Hash Interactive
Easy peasy ! Now in all of your components you can use Tailwind CSS utility classes, or in your imported css files you...
Read more >
Learning how to code with Gatsby
Easy peasy. They want to build for production they use $ gatsby build && gatsby serve . Simple! Netlify: When I showed them...
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