Store 'someStore' is not available! Make sure it is provided by some Provider

Englund0110 picture Englund0110 · Mar 25, 2018 · Viewed 9.6k times · Source

I am trying to build a new projec twith React, Typescript and MobX.

For some reason, i cant get MobX to work. It is a relative simple piece of code, but it gives me this error.

Uncaught Error: MobX injector: Store 'appState' is not available! Make sure it is provided by some Provider

This is my code:

AppState.ts

import {observable} from "mobx"
import {observer} from "mobx-react"

export class AppState {
    @observable public greeting = "hello World"
}

index.tsx

import * as React from "react"
import * as ReactDOM from "react-dom"
import { Provider } from "mobx-react"
import { Router, Route, Switch } from "react-router"
import { createBrowserHistory } from "history"
import MuiThemeProvider from "material-ui/styles/MuiThemeProvider"
"./containers/BookingStep/BookingStepContainer";
import { TestContainer } from "./containers/TestContainer";
import { AppState } from "./store/AppState";

const history = createBrowserHistory()

ReactDOM.render(
  <Provider store={AppState}>
    <Router history={history}>
      <Switch>
        <MuiThemeProvider>
          <div>
            <Route path="/test" component={TestContainer} />
          </div>
        </MuiThemeProvider>
      </Switch>
  </Router>
</Provider>,
document.getElementById("root"))

TestContainer.tsx

import * as React from "react"
import { inject } from "mobx-react"
import { AppState } from "../store/AppState"

export interface ITestContainerProps {
    appState?: AppState
}

@inject("appState")
export class TestContainer extends React.Component<ITestContainerProps, any> {

    public render() {
        const { children, appState } = this.props

        return (
            <div>
                {appState.greeting}
            </div>
        )
    }
}

Can anyone point out what i am missing?

Answer

ethan.roday picture ethan.roday · Mar 26, 2018

You're asking the injector to look for a store named appState, but what you've given the Provider is a store named store. It looks like you need to change this line:

<Provider store={AppState}>

to:

<Provider appState={AppState}>