Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 417
Englund0110 Avatar asked Mar 25 '18 17:03

Englund0110


1 Answers

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}>
like image 89
ethan.roday Avatar answered Nov 03 '22 19:11

ethan.roday