Skip to content

Can I persist environments that I pass to the tree on creation? #1990

Answered by EmilTholin
hamzakat asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @hamzakat!

The environment can not be part of the model snapshot sadly, so the axios API instance that you inject can not be persisted in that way. You could however sync the API configs with e.g. localStorage outside of MST and use this potential value in localStorage on initialization to get around this.

It is often useful to have the token(s) be part of a model so that you can e.g. render logged in/logged out views depending on if the user is authenticated, so something like this might give some inspiration:

Example

import axios from "axios";
import { flow, types as t, getRoot } from "mobx-state-tree";

export const AuthStore = t
  .model("AuthStore", {
    accessToken: t.maybeNull(t.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by hamzakat
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants