-
Notifications
You must be signed in to change notification settings - Fork 2
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
16 changed files
with
203 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# README | ||
|
||
It's a demo which help developer to understand the react-mobx project structure. |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* @desc Pure React Component (Can reuse in other project) | ||
* @ai This code snippet defines a React component called CounterAtom. | ||
* It uses the useState hook to create a local state variable called count and a function called setCount to update it. | ||
* The useEffect hook is used to execute a timer that increments the count variable every second. | ||
* The component renders a paragraph element displaying the current value of count. | ||
*/ | ||
export const CounterAtom = () => { | ||
// Local state (React) | ||
const [count, setCount] = useState(0); | ||
|
||
// Local counter | ||
useEffect(() => { | ||
const timerId = setInterval(() => setCount((n) => n + 1), 1000); | ||
return () => clearInterval(timerId); | ||
}, []); | ||
|
||
return <p>Local counter: {count} tick</p>; | ||
}; |
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,11 @@ | ||
import { memo } from 'react'; | ||
|
||
/** | ||
* @desc Pure React Component (Can reuse in other project) | ||
* @desc React memo example | ||
*/ | ||
export const HeadingAtom = memo(() => { | ||
// Check console message in devtool | ||
console.log('[debug] Every component should be memoized and log once'); | ||
return <h2>Hello world! (Static Content)</h2>; | ||
}); |
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,43 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { CounterAtom } from './Counter.Atom'; | ||
import { HeadingAtom } from './Heading.Atom'; | ||
|
||
interface ITimerAtomProps { | ||
timePassed: number; | ||
tip?: string; | ||
node?: HTMLElement; | ||
} | ||
|
||
/** | ||
* @desc Pure React Component (Can reuse in other project) | ||
* @ai This code snippet defines a React component called TimerAtom. | ||
* It takes in props node, timePassed, and tip. Inside the component, it creates a reference using the useRef hook. | ||
* It also uses the useEffect hook to append the node to the ref.current element when the component mounts. | ||
* The useEffect hook also returns a cleanup function that removes the node from the ref.current element when the component unmounts. | ||
* The component renders a CounterAtom component and a paragraph element that displays the timePassed prop. | ||
* If the tip prop is provided, it appends the tip value to the paragraph element. | ||
*/ | ||
export const TimerAtom = ({ node, timePassed, tip }: ITimerAtomProps) => { | ||
const ref = useRef<HTMLParagraphElement>(); | ||
|
||
useEffect(() => { | ||
node && ref.current.append(node); | ||
return () => node && node.remove(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{/* It's a memo component */} | ||
<HeadingAtom /> | ||
|
||
{/* It's a pure react component */} | ||
{/* DON'T USE THIS in Mobx, it's only a demo */} | ||
{/* Wrap it with Mobx.observer if you want to use it in mobx environment */} | ||
<CounterAtom /> | ||
|
||
<p ref={ref}> | ||
Time passed: {timePassed}s{tip ? ` (${tip})` : null} | ||
</p> | ||
</> | ||
); | ||
}; |
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,36 @@ | ||
# Example | ||
|
||
## 1. Multi File | ||
|
||
file1.tsx | ||
|
||
```tsx | ||
export const ExampleAtom = () => <p>example</p> | ||
``` | ||
|
||
file2.tsx | ||
|
||
```tsx | ||
import { observer } from 'mobx-react-lite'; | ||
import { ExampleAtom } from './file1'; | ||
|
||
export const Example = observer(ExampleAtom) | ||
``` | ||
|
||
## 2. Single File (decoupling) | ||
|
||
```tsx | ||
import { observer } from 'mobx-react-lite'; | ||
|
||
const ExampleAtom = () => <p>example</p> | ||
|
||
export const Example = observer(ExampleAtom) | ||
``` | ||
|
||
## 3. Single File (coupling) | ||
|
||
```tsx | ||
import { observer } from 'mobx-react-lite'; | ||
|
||
export const Example = observer(() => <p>example</p>) | ||
``` |
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,25 @@ | ||
import { observer } from 'mobx-react-lite'; | ||
import { useStore } from '../../context'; | ||
import { useEffect } from 'react'; | ||
import { themeStore } from '../../store/global/theme.store'; | ||
|
||
export const ThemeSwitchView = observer(() => { | ||
useEffect(() => { | ||
document.body.dataset.theme = themeStore.theme; | ||
}, [themeStore.theme]); | ||
|
||
return <button onClick={() => themeStore.toggle()}>Switch Theme (import global store)</button>; | ||
}); | ||
|
||
export const ThemeSwitchView2 = observer(() => { | ||
/** | ||
* Use ReactContext instead of import variable. | ||
*/ | ||
const { themeStore } = useStore(); | ||
|
||
useEffect(() => { | ||
document.body.dataset.theme = themeStore.theme; | ||
}, [themeStore.theme]); | ||
|
||
return <button onClick={() => themeStore.toggle()}>Switch Theme2 (useContext)</button>; | ||
}); |
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.
5 changes: 4 additions & 1 deletion
5
apps/react-demo/src/store/timer.store.ts → apps/react-demo/src/store/app/timer.store.ts
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
5 changes: 4 additions & 1 deletion
5
apps/react-demo/src/store/ui.store.ts → apps/react-demo/src/store/app/ui.store.ts
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Example | ||
|
||
| State Scope | Description | | ||
|------------------|---------------------------------------------------------| | ||
| Local | Component private state | | ||
| App (Instance) | App shared state (Partial shared or App shared) | | ||
| Global (Sington) | Global state for multi app instances and all components | |
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,18 @@ | ||
import { makeAutoObservable } from 'mobx'; | ||
|
||
/** | ||
* @desc Global Store (Singleton) | ||
*/ | ||
class ThemeStore { | ||
theme = 'light'; | ||
|
||
constructor() { | ||
makeAutoObservable(this); | ||
} | ||
|
||
toggle() { | ||
this.theme = this.theme === 'light' ? 'dark' : 'light'; | ||
} | ||
} | ||
|
||
export const themeStore = new ThemeStore(); |
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,7 +1,10 @@ | ||
import { TimerStore } from './timer.store'; | ||
import { UiStore } from './ui.store'; | ||
import { TimerStore } from './app/timer.store'; | ||
import { UiStore } from './app/ui.store'; | ||
import { themeStore } from './global/theme.store'; | ||
|
||
export class RootStore { | ||
themeStore = themeStore; | ||
|
||
uiStore = new UiStore(this); | ||
timerStore = new TimerStore(this); | ||
} |
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,9 @@ | ||
body[data-theme="light"] { | ||
background-color: #fff; | ||
color: #000; | ||
} | ||
|
||
body[data-theme="dark"] { | ||
background-color: #000; | ||
color: #fff; | ||
} |