问题
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?
回答1:
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}>
来源:https://stackoverflow.com/questions/49478518/store-somestore-is-not-available-make-sure-it-is-provided-by-some-provider