Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to integrate an observable object into MST #161

Closed
yuluyi opened this issue May 31, 2017 · 3 comments
Closed

How to integrate an observable object into MST #161

yuluyi opened this issue May 31, 2017 · 3 comments
Labels
question Question or help request

Comments

@yuluyi
Copy link

yuluyi commented May 31, 2017

For example, I wan't to sync the state of react router to MST. I tried the way mobx-react-router does.

class Routing {
    @observable location?: LocationDescriptorObject
    history?: History 
    constructor(history: History) {
        this.history = history
        history.listen(this._updateLocation.bind(this))
        this._updateLocation(history.location)
    }

    @action
    _updateLocation(newState: LocationDescriptorObject) {
        this.location = newState
    }
    ...
}

const Store = types.model({
    routing: types.frozen
})

const history = createBrowserHistory()

const storeInstance = Store.create({
    routing: new Routing(history)
})

onSnapshot(storeInstance, snapshot => console.log(snapshot))

ReactDOM.render(
    <Provider store={storeInstance}>
        <Router history={history}>
            {...}
        </Router>
    </Provider>
)

The callback of onSnapshot will never be triggered. I think it's by design of types.frozen. But is there any way to allow an MST reacting to the state of react-router?

@mattiamanzati
Copy link
Contributor

mattiamanzati commented May 31, 2017

Hello! You cannot pass in classes in MST type definitions, and as said internal updates in frozen types will not trigger snapshots. You need to replace entirely the frozen property. Please try something like this! :)

// define the store
const Store = types.model({
    locationState: types.maybe(types.frozen)
}, {
    updateLocationState(newState){
      this.locationState = newState
    }
})

// create the router
const history = createBrowserHistory()
// create the store instance
const storeInstance = Store.create()
// hook them up
history.listen(newState => storeInstance.updateLocationState(newState))

onSnapshot(storeInstance, snapshot => console.log(snapshot))

ReactDOM.render(
    <Provider store={storeInstance}>
        <Router history={history}>
            {...}
        </Router>
    </Provider>
)

@mattiamanzati mattiamanzati added the question Question or help request label May 31, 2017
@yuluyi
Copy link
Author

yuluyi commented Jun 1, 2017

@mattiamanzati Got it. Thanks. Is there any plan to support observable classes in MST? So we can use mobx within MST.

@mattiamanzati
Copy link
Contributor

It mostly probably wont, see #3 and #136 for motivations :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or help request
Projects
None yet
Development

No branches or pull requests

2 participants