Skip to content

Instantly share code, notes, and snippets.

@tabekg
Created December 11, 2018 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabekg/f55b28ebca509b51bedf17d6467c4038 to your computer and use it in GitHub Desktop.
Save tabekg/f55b28ebca509b51bedf17d6467c4038 to your computer and use it in GitHub Desktop.
React, Mobx error: TypeError: _this.store.setBusyState is not a function
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;
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;
}
}
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