我正在尝试构建一个新的项目React,Typescript和MobX。
出于某种原因,我无法让MobX工作。这是一段相对简单的代码,但它给了我这个错误。
未捕获错误:MobX进样器:存储'appState'不可用!确保它由某些提供商提供
这是我的代码:
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>
)
}
}
有人能说出我错过的东西吗?
答案 0 :(得分:7)
您要求注射器查找名为appState
的商店,但您给Provider
的商店是名为store
的商店。看起来您需要更改此行:
<Provider store={AppState}>
为:
<Provider appState={AppState}>