├── .gitignore ├── .idea ├── .gitignore ├── inspectionProfiles │ └── Project_Default.xml ├── misc.xml ├── modules.xml ├── react_hooks_mobx.iml └── vcs.xml ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.css ├── App.test.tsx ├── App.tsx ├── features │ ├── TodoItem.tsx │ ├── TodoList.tsx │ └── TodoNew.tsx ├── helpers │ ├── store-provider.ts │ ├── use-enter.ts │ └── use-store.ts ├── index.css ├── index.tsx ├── logo.svg ├── react-app-env.d.ts ├── serviceWorker.ts ├── setupTests.ts └── stores │ ├── todo-item.ts │ └── todo-list.ts ├── tsconfig.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml 3 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/react_hooks_mobx.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## React Hooks + Mobx Guide 2 | 3 | Link to Example: http://react-hooks-mobx.georgy-glezer.com
4 | Link to detailed guide: 5 | 6 | # Build Stats 7 | 8 | ``` 9 | 10 | File sizes after gzip: 11 | 12 | 54.13 KB build\static\js\2.f209cf74.chunk.js 13 | 1.46 KB build\static\js\main.2336ad02.chunk.js 14 | 779 B build\static\js\runtime-main.bae833a8.js 15 | 656 B build\static\css\main.1b00465a.chunk.css 16 | ``` 17 | 18 | 19 | 20 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 21 | ## Available Scripts 22 | 23 | In the project directory, you can run: 24 | 25 | ### `yarn start` 26 | 27 | Runs the app in the development mode.
28 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 29 | 30 | The page will reload if you make edits.
31 | You will also see any lint errors in the console. 32 | 33 | ### `yarn test` 34 | 35 | Launches the test runner in the interactive watch mode.
36 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 37 | 38 | ### `yarn build` 39 | 40 | Builds the app for production to the `build` folder.
41 | It correctly bundles React in production mode and optimizes the build for the best performance. 42 | 43 | The build is minified and the filenames include the hashes.
44 | Your app is ready to be deployed! 45 | 46 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 47 | 48 | ### `yarn eject` 49 | 50 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 51 | 52 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 53 | 54 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 55 | 56 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 57 | 58 | ## Learn More 59 | 60 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 61 | 62 | To learn React, check out the [React documentation](https://reactjs.org/). 63 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react_hooks_mobx", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "mobx": "^6.3.2", 7 | "mobx-react": "^7.2.0", 8 | "react": "^16.12.0", 9 | "react-dom": "^16.12.0", 10 | "react-scripts": "3.3.0" 11 | }, 12 | "devDependencies": { 13 | "@testing-library/jest-dom": "^4.2.4", 14 | "@testing-library/react": "^9.3.2", 15 | "@testing-library/user-event": "^7.1.2", 16 | "@types/jest": "^24.0.24", 17 | "@types/node": "^12.12.21", 18 | "@types/react": "^16.9.17", 19 | "@types/react-dom": "^16.9.4", 20 | "typescript": "^3.7.4" 21 | }, 22 | "scripts": { 23 | "start": "react-scripts start", 24 | "build": "react-scripts build", 25 | "test": "react-scripts test", 26 | "eject": "react-scripts eject" 27 | }, 28 | "eslintConfig": { 29 | "extends": "react-app" 30 | }, 31 | "browserslist": { 32 | "production": [ 33 | ">0.2%", 34 | "not dead", 35 | "not op_mini all" 36 | ], 37 | "development": [ 38 | "last 1 chrome version", 39 | "last 1 firefox version", 40 | "last 1 safari version" 41 | ] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stolenng/react-hooks-mobx/874696a7f081cc8107461e379118c38d144b6100/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stolenng/react-hooks-mobx/874696a7f081cc8107461e379118c38d144b6100/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stolenng/react-hooks-mobx/874696a7f081cc8107461e379118c38d144b6100/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/App.test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './App.css'; 3 | import {TodoList} from "./features/TodoList"; 4 | import {TodoNew} from "./features/TodoNew"; 5 | 6 | const App = () => { 7 | return ( 8 |
9 | 10 | 11 |
12 | ); 13 | } 14 | 15 | export default App; 16 | -------------------------------------------------------------------------------- /src/features/TodoItem.tsx: -------------------------------------------------------------------------------- 1 | import React, {useState} from 'react'; 2 | import TodoItemClass from "../stores/todo-item"; 3 | import {useStore} from "../helpers/use-store"; 4 | import {onEnterPress} from "../helpers/use-enter"; 5 | 6 | interface Props { 7 | todo: TodoItemClass; 8 | } 9 | 10 | export const TodoItem = ({todo}: Props) => { 11 | const todoList = useStore(); 12 | const [newText, setText] = useState(''); 13 | const [isEditing, setEdit] = useState(false); 14 | 15 | const saveText = () => { 16 | todo.updateText(newText); 17 | setEdit(false); 18 | setText(''); 19 | }; 20 | 21 | return ( 22 |
23 | { 24 | isEditing ? 25 |
26 | setText(e.target.value)}/> 27 | 28 |
29 | : 30 |
31 | {todo.text} 32 | 33 | 34 | 35 |
36 | } 37 |
38 | ) 39 | }; 40 | -------------------------------------------------------------------------------- /src/features/TodoList.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import {useStore} from "../helpers/use-store"; 3 | import {TodoItem} from "./TodoItem"; 4 | import {useObserver} from "mobx-react"; 5 | 6 | export const TodoList = () => { 7 | const todoList = useStore(); 8 | 9 | return useObserver(() => ( 10 |
11 |
12 | Open Todos 13 | {todoList.openTodos.map(todo => )} 14 |
15 |
16 | Finished Todos 17 | {todoList.finishedTodos.map(todo => )} 18 |
19 |
20 | )); 21 | }; 22 | -------------------------------------------------------------------------------- /src/features/TodoNew.tsx: -------------------------------------------------------------------------------- 1 | import React, {useState} from 'react'; 2 | import {useStore} from "../helpers/use-store"; 3 | import {onEnterPress} from "../helpers/use-enter"; 4 | 5 | export const TodoNew = () => { 6 | const [newTodo, setTodo] = useState(''); 7 | const todoList = useStore(); 8 | 9 | const addTodo = () => { 10 | todoList.addTodo(newTodo); 11 | setTodo(''); 12 | }; 13 | 14 | return ( 15 |
16 | setTodo(e.target.value)}/> 17 | 18 |
19 | ) 20 | }; 21 | -------------------------------------------------------------------------------- /src/helpers/store-provider.ts: -------------------------------------------------------------------------------- 1 | import { createContext } from 'react'; 2 | import {TodoList} from "../stores/todo-list"; 3 | 4 | export const StoreContext = createContext({} as TodoList); 5 | export const StoreProvider = StoreContext.Provider; 6 | -------------------------------------------------------------------------------- /src/helpers/use-enter.ts: -------------------------------------------------------------------------------- 1 | import {KeyboardEvent} from "react"; 2 | 3 | export const onEnterPress = (cb: any) => { 4 | return (e: KeyboardEvent) => { 5 | if (e.key === 'Enter') { 6 | cb(); 7 | } 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /src/helpers/use-store.ts: -------------------------------------------------------------------------------- 1 | import { useContext } from 'react'; 2 | import { StoreContext } from './store-provider'; 3 | import {TodoList} from "../stores/todo-list"; 4 | 5 | export const useStore = (): TodoList => useContext(StoreContext); 6 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding:15px; 4 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 5 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 6 | sans-serif; 7 | -webkit-font-smoothing: antialiased; 8 | -moz-osx-font-smoothing: grayscale; 9 | } 10 | 11 | code { 12 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 13 | monospace; 14 | } 15 | 16 | .todo-new { 17 | font-size: 20px; 18 | margin-bottom: 30px; 19 | } 20 | 21 | .todo-new button { 22 | font-size: 15px; 23 | } 24 | .todo-new input { 25 | margin-right: 15px; 26 | } 27 | 28 | .todo-item { 29 | font-size: 20px; 30 | } 31 | 32 | .todo-item span { 33 | width: 350px; 34 | text-align: left; 35 | display: inline-block 36 | } 37 | 38 | .open-todos { 39 | margin-bottom: 30px; 40 | } 41 | 42 | .todo-item input { 43 | margin-left: 15px; 44 | margin-right: 15px; 45 | } 46 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | import {StoreProvider} from "./helpers/store-provider"; 7 | import {TodoList} from "./stores/todo-list"; 8 | 9 | const todoList = new TodoList([ 10 | 'Should Starting Writing in React', 11 | 'Should Learn MobX', 12 | 'Should Watch Once Piece :)' 13 | ]); 14 | 15 | //@ts-ignore - for debugging 16 | window.todoList = todoList 17 | 18 | ReactDOM.render( 19 | 20 | 21 | 22 | , document.getElementById('root')); 23 | 24 | // If you want your app to work offline and load faster, you can change 25 | // unregister() to register() below. Note this comes with some pitfalls. 26 | // Learn more about service workers: https://bit.ly/CRA-PWA 27 | serviceWorker.unregister(); 28 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /src/serviceWorker.ts: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | type Config = { 24 | onSuccess?: (registration: ServiceWorkerRegistration) => void; 25 | onUpdate?: (registration: ServiceWorkerRegistration) => void; 26 | }; 27 | 28 | export function register(config?: Config) { 29 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 30 | // The URL constructor is available in all browsers that support SW. 31 | const publicUrl = new URL( 32 | process.env.PUBLIC_URL, 33 | window.location.href 34 | ); 35 | if (publicUrl.origin !== window.location.origin) { 36 | // Our service worker won't work if PUBLIC_URL is on a different origin 37 | // from what our page is served on. This might happen if a CDN is used to 38 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 39 | return; 40 | } 41 | 42 | window.addEventListener('load', () => { 43 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 44 | 45 | if (isLocalhost) { 46 | // This is running on localhost. Let's check if a service worker still exists or not. 47 | checkValidServiceWorker(swUrl, config); 48 | 49 | // Add some additional logging to localhost, pointing developers to the 50 | // service worker/PWA documentation. 51 | navigator.serviceWorker.ready.then(() => { 52 | console.log( 53 | 'This web app is being served cache-first by a service ' + 54 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 55 | ); 56 | }); 57 | } else { 58 | // Is not localhost. Just register service worker 59 | registerValidSW(swUrl, config); 60 | } 61 | }); 62 | } 63 | } 64 | 65 | function registerValidSW(swUrl: string, config?: Config) { 66 | navigator.serviceWorker 67 | .register(swUrl) 68 | .then(registration => { 69 | registration.onupdatefound = () => { 70 | const installingWorker = registration.installing; 71 | if (installingWorker == null) { 72 | return; 73 | } 74 | installingWorker.onstatechange = () => { 75 | if (installingWorker.state === 'installed') { 76 | if (navigator.serviceWorker.controller) { 77 | // At this point, the updated precached content has been fetched, 78 | // but the previous service worker will still serve the older 79 | // content until all client tabs are closed. 80 | console.log( 81 | 'New content is available and will be used when all ' + 82 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 83 | ); 84 | 85 | // Execute callback 86 | if (config && config.onUpdate) { 87 | config.onUpdate(registration); 88 | } 89 | } else { 90 | // At this point, everything has been precached. 91 | // It's the perfect time to display a 92 | // "Content is cached for offline use." message. 93 | console.log('Content is cached for offline use.'); 94 | 95 | // Execute callback 96 | if (config && config.onSuccess) { 97 | config.onSuccess(registration); 98 | } 99 | } 100 | } 101 | }; 102 | }; 103 | }) 104 | .catch(error => { 105 | console.error('Error during service worker registration:', error); 106 | }); 107 | } 108 | 109 | function checkValidServiceWorker(swUrl: string, config?: Config) { 110 | // Check if the service worker can be found. If it can't reload the page. 111 | fetch(swUrl, { 112 | headers: { 'Service-Worker': 'script' } 113 | }) 114 | .then(response => { 115 | // Ensure service worker exists, and that we really are getting a JS file. 116 | const contentType = response.headers.get('content-type'); 117 | if ( 118 | response.status === 404 || 119 | (contentType != null && contentType.indexOf('javascript') === -1) 120 | ) { 121 | // No service worker found. Probably a different app. Reload the page. 122 | navigator.serviceWorker.ready.then(registration => { 123 | registration.unregister().then(() => { 124 | window.location.reload(); 125 | }); 126 | }); 127 | } else { 128 | // Service worker found. Proceed as normal. 129 | registerValidSW(swUrl, config); 130 | } 131 | }) 132 | .catch(() => { 133 | console.log( 134 | 'No internet connection found. App is running in offline mode.' 135 | ); 136 | }); 137 | } 138 | 139 | export function unregister() { 140 | if ('serviceWorker' in navigator) { 141 | navigator.serviceWorker.ready.then(registration => { 142 | registration.unregister(); 143 | }); 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /src/setupTests.ts: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /src/stores/todo-item.ts: -------------------------------------------------------------------------------- 1 | import {action, makeObservable, observable} from "mobx"; 2 | 3 | export default class TodoItem { 4 | id = Date.now(); 5 | 6 | @observable text: string = ''; 7 | @observable isDone: boolean = false; 8 | 9 | constructor(text: string) { 10 | makeObservable(this); 11 | this.text = text; 12 | } 13 | 14 | @action 15 | toggleIsDone = () => { 16 | this.isDone = !this.isDone 17 | } 18 | 19 | @action 20 | updateText = (text: string) => { 21 | this.text = text; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/stores/todo-list.ts: -------------------------------------------------------------------------------- 1 | import {action, computed, makeObservable, observable} from "mobx"; 2 | import TodoItem from "./todo-item"; 3 | 4 | export class TodoList { 5 | @observable.shallow list: TodoItem[] = []; 6 | 7 | constructor(todos: string[]) { 8 | makeObservable(this); 9 | todos.forEach(this.addTodo); 10 | } 11 | 12 | @action 13 | addTodo = (text: string) => { 14 | this.list.push(new TodoItem(text)); 15 | } 16 | 17 | @action 18 | removeTodo = (todo: TodoItem) => { 19 | this.list.splice(this.list.indexOf(todo), 1); 20 | }; 21 | 22 | @computed 23 | get finishedTodos(): TodoItem[] { 24 | return this.list.filter(todo => todo.isDone); 25 | } 26 | 27 | @computed 28 | get openTodos(): TodoItem[] { 29 | return this.list.filter(todo => !todo.isDone); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "experimentalDecorators": true, 10 | "allowJs": true, 11 | "skipLibCheck": true, 12 | "esModuleInterop": true, 13 | "allowSyntheticDefaultImports": true, 14 | "strict": true, 15 | "forceConsistentCasingInFileNames": true, 16 | "module": "esnext", 17 | "moduleResolution": "node", 18 | "resolveJsonModule": true, 19 | "isolatedModules": true, 20 | "noEmit": true, 21 | "jsx": "react" 22 | }, 23 | "include": [ 24 | "src" 25 | ] 26 | } 27 | --------------------------------------------------------------------------------