Created
December 11, 2018 10:11
-
-
Save tabekg/f55b28ebca509b51bedf17d6467c4038 to your computer and use it in GitHub Desktop.
React, Mobx error: TypeError: _this.store.setBusyState is not a function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { BrowserRouter as Router, Route } from 'react-router-dom'; | |
import LazyRoute from './components/ui/LazyRoute'; | |
import {Provider} from 'mobx-react'; | |
import AppStore from './stores/AppStore'; | |
import Home from './components/Home'; | |
class App extends Component { | |
render() { | |
return ( | |
<Provider AppStore={AppStore}> | |
<Router> | |
<div> | |
<Route path="/" exact component={Home} /> | |
<Route path="/login" render={props => <LazyRoute {...props} component={import("./components/Login")} />} /> | |
</div> | |
</Router> | |
</Provider> | |
); | |
} | |
} | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {observable, computed, action} from 'mobx'; | |
export default class AppStore { | |
@observable busyCount = 0; | |
@observable auth = false; | |
@action setBusyState(state){ | |
this.busyCount += state ? 1 : -1; | |
} | |
@computed get isBusy(){ | |
return this.busyCount > 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react'; | |
import {inject} from 'mobx-react'; | |
@inject('AppStore') | |
class LazyRoute extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { status: 'loading' }; | |
this.store = this.props.AppStore; | |
this.store.setBusyState(true); | |
} | |
componentDidMount() { | |
this.props.component.then((module) => { | |
this.component = module.default; | |
this.setState({ status: 'loaded' }); | |
this.store.setBusyState(false); | |
}) | |
} | |
render() { | |
const {status} = this.state; | |
const {loading} = this.props; | |
if (status === 'loaded') { | |
return <this.component {...this.props} /> | |
} else if (loading) { | |
return <loading/> | |
} | |
} | |
} | |
export default LazyRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment