-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
50 additions
and
56 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { observer } from 'mobx-react-lite'; | ||
import { useEffect } from 'react'; | ||
import { themeStore } from '../../store/global/theme.store'; | ||
import { useThemeStore } from '../../store/global/theme.store'; | ||
|
||
export const MobxThemeSwitch = observer(() => { | ||
const themeStore = useThemeStore(); | ||
|
||
useEffect(() => { | ||
document.body.dataset.theme = themeStore.theme; | ||
}, [themeStore.theme]); | ||
|
||
return ( | ||
<p> | ||
<button onClick={() => themeStore.toggle()}>Switch Theme (Global Level: import global store)</button> | ||
<button onClick={() => themeStore.toggle()}>Switch Theme (Global Level)</button> | ||
</p> | ||
); | ||
}); |
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import { makeAutoObservable } from 'mobx'; | ||
import { RootStore } from '../root.store'; | ||
import { defineStore } from '../defineStore'; | ||
|
||
/** | ||
* @desc App Store | ||
*/ | ||
export class SubmitStore { | ||
class SubmitStore { | ||
submitted = false; | ||
|
||
constructor(readonly rootStore: RootStore) { | ||
constructor() { | ||
makeAutoObservable(this); | ||
} | ||
|
||
toggle() { | ||
this.submitted = !this.submitted; | ||
} | ||
} | ||
|
||
export const useSubmitStore = defineStore('submit', SubmitStore); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { RootStore } from '../root.store'; | ||
import { makeAutoObservable } from 'mobx'; | ||
import { defineStore } from '../defineStore'; | ||
|
||
/** | ||
* @desc App Store | ||
*/ | ||
export class UiStore { | ||
constructor(readonly rootStore: RootStore) { | ||
class UiStore { | ||
constructor() { | ||
makeAutoObservable(this); | ||
} | ||
} | ||
|
||
export const useUiStore = defineStore('ui', UiStore); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createContext } from 'react'; | ||
|
||
export const RootContext = createContext<Map<string, unknown> | null>(null); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { RootContext } from './context'; | ||
import { useContext } from 'react'; | ||
|
||
export function defineStore<T extends new (...args: unknown[]) => InstanceType<T>>(id: string, Ctor: T) { | ||
return (injectedContext?: Map<string, unknown> | null): InstanceType<T> => { | ||
const map = injectedContext || useContext(RootContext); | ||
|
||
if (!map) { | ||
throw new Error('[defineStore] must be used within a Provider'); | ||
} | ||
|
||
if (map.has(id)) { | ||
return map.get(id) as InstanceType<T>; | ||
} else { | ||
const instance = new Ctor(); | ||
map.set(id, instance); | ||
return instance; | ||
} | ||
}; | ||
} |
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
This file was deleted.
Oops, something went wrong.