1

Trying to get Mobx running with my react app. When I wrap the entire app in the RootStoreProvider I get the following error: Error: Objects are not valid as a React child (found: object with keys {$$typeof, type, compare}). If you meant to render a collection of children, use an array instead. Whatever children is its not a react component and I can't quite figure out why (I am aware this issue is because children is an object)... I am trying to follow several example tutorials here and here which all seem to have the same function more or less:

export function RootStoreProvider({ children }: { children: ReactNode }) {
  // only create root store once (store is a singleton)
  const root = store ?? new RootStore();

  return <StoreContext.Provider value={root}>{children}</StoreContext.Provider>;
}

Here is what my index.tsx file looks like:

ReactDOM.render(
  <React.StrictMode>
    <BrowserRouter>
      <RootStoreProvider>
        <App />
      </RootStoreProvider>
    </BrowserRouter>
  </React.StrictMode>,
  document.getElementById('root')
);

Below I am including all my store/provider files:

RootStore.tsx

import React from 'react';
import DeckStore from './DeckStore';

export default class RootStore {
  deckStore: DeckStore;

  constructor() {
    this.deckStore = DeckStore(this);
  }
}

DeckStore.tsx

import { RootStore } from './RootStore';
import { makeAutoObservable } from 'mobx';

const DeckStore = (root: RootStore) => {
  return makeAutoObservable({
    decks: [{
      title: 'somedeck'
    }],
    addDeck: (deck) => {
      root.deckStore.decks.push(deck);
    },
  });
}

export default DeckStore;

provider.tsx

import React, { createContext, ReactNode, useContext } from "react";
import RootStore from "../stores/RootStore";

let store: RootStore;
const StoreContext = createContext<RootStore | undefined>(undefined);
StoreContext.displayName = "StoreContext";

export function useRootStore() {
  const context = useContext(StoreContext);
  if (context === undefined) {
    throw new Error("useRootStore must be used within RootStoreProvider");
  }

  return context;
}

export const useDeckStore = () => {
  const { deckStore } = useRootStore();
  return deckStore;
}

export function RootStoreProvider({ children }: { children: ReactNode }) {
  // only create root store once (store is a singleton)
  const root = store ?? new RootStore();

  return <StoreContext.Provider value={root}>{children}</StoreContext.Provider>;
}
Szpok
  • 53
  • 1
  • 1
  • 7
  • How are you using this store? Maybe the problem is inside `App` component, have you tried to remove it to see if the error goes away? – Danila Aug 30 '21 at 08:34
  • I am not using it at all right now... I am just trying to integrate Mobx into my application and get it to load. To answer your question, what I delete `` it loads a blank page without any errors. – Szpok Sep 05 '21 at 05:31
  • If you are not using the store then the problem is not inside the store obviously. Check your `App` component and all its children. – Danila Sep 05 '21 at 11:58

0 Answers0