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.

Is it possible to use mobx with gatsby without using client side routing? I’ve taken a look at https://github.com/Shaid/gatsby-example-mobx but it uses react-router client side routing. I assume those routes won’t be picked up by gatsby and created as static html files?

At the moment ive tried to wrap my layouts/index.js in <Provider> from mobx-react, and use @inject in pages/index.js but it won’t work.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
jonniebigodescommented, Sep 13, 2018

@KyleAMathews and to whom it may concern. i saw this issue being bounced around a bit so i took a crack at it. Grabbed the example from redux and implemented it using mobx i have a working example in here . If you guys want it add that to the examples. Thanks in advance

4reactions
TomiTakussaaricommented, Feb 1, 2018

This works for me:

#gatsby-ssr.js

import { Provider, useStaticRendering } from "mobx-react";
import { renderToString } from "react-dom/server";
import MyStore from "../src/stores/MyStore";

exports.replaceRenderer = ({ bodyComponent, replaceBodyHTMLString }) => {
    useStaticRendering(true);

    const ProviderBody = () => (
        <Provider Store={MyStore}>
                {bodyComponent}
        </Provider>
    );

    replaceBodyHTMLString(renderToString(<ProviderBody />));
};

# gatsby-browser.js

import { Router } from "react-router-dom";
import { Provider } from "mobx-react";
import MyStore from "../src/stores/MyStore";

exports.replaceRouterComponent = ({ history }) => {
    return ({ children }) => (
        <Provider Store={MyStore}>
                <Router history={history}>{children}</Router>
        </Provider>
    );
};
#component.js

@inject("Store")
@observer
export default class MyComponent extends Component {
...
}
# src/stores/MyStore.js

import {observable} from 'mobx';

export class MyStore {
    @observable myState = {};
    ....
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

MobX + Gatsby - Doug Waltman
MobX stores are a great way to manage shared state in a Gatsby project, and can even be used to abstract async fetch...
Read more >
jonniebigodes/gatsby-example-mobx - GitHub
To use mobx in a Gatsby site you'll need to hook in to two of Gatsby's extension points. Once in wrapRootElement which runs...
Read more >
Learn Gatsby, Mobx and Svelte - Egghead.io
course. Source and Create Nodes with Data from an API in Gatsby to Create a Pokemon Pokedex. Colby Fayock. course by Colby Fayock....
Read more >
@borekb/gatsby-starter-mobx - CodeSandbox
borekb/gatsby-starter-mobx. This Sandbox is in sync with master on GitHub. You have to fork to make changes. Fork. Files. jest. src .babelrc .editorconfig....
Read more >
Gatsby Starter Mobx - Demo - Jamstack Themes
Gatsby Starter Mobx. Gatsby · Theme Info Github. Gatsby MobX starter. counter example · temperature example.
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