Skip to content

Save MobX state in LocalStorage #2798

Discussion options

You must be logged in to vote

This is the code that works. toJSON contains the properties that need to be saved. Let me know if anything can be improved :)

Here's the Codesandbox → https://codesandbox.io/s/mobx-persist-store-manual-implementation-vm38r?file=/src/store.ts

import { observable, action, makeObservable, computed, autorun } from "mobx"
import pick from "lodash.pick"

type Color = "red" | "blue" | "green"

export interface ICounterStore {
	counter: number
	color: Color
	switchColor(color: Color): void
	increment(n: number): void
	decrement(n: number): void
}

export class CounterStore implements ICounterStore {
	// private state = ""
	counter = 0
	color: Color = "red"

	constructor() {
		makeObservable(this, {

Replies: 4 comments 16 replies

Comment options

You must be logged in to vote
15 replies
@deadcoder0904
Comment options

@deadcoder0904
Comment options

@deadcoder0904
Comment options

@urugator
Comment options

@deadcoder0904
Comment options

Comment options

You must be logged in to vote
1 reply
@deadcoder0904
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

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