├── src ├── index.css ├── index.js ├── Providers │ └── TimeProvider.js ├── App.test.js ├── Components │ ├── Time.js │ ├── TaskApp.js │ ├── Notification.js │ ├── TaskForm.js │ └── TaskList.js ├── App.css ├── ReStated │ └── index.js ├── App.js ├── logo.svg └── registerServiceWorker.js ├── public ├── favicon.ico ├── manifest.json └── index.html ├── .gitignore ├── package.json └── README.md /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajeshpillai/react-restated/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import registerServiceWorker from './registerServiceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | registerServiceWorker(); 9 | -------------------------------------------------------------------------------- /src/Providers/TimeProvider.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import {Container} from '../ReStated'; 3 | 4 | export default class TimeProvider extends Container { 5 | state = { 6 | time: new Date() 7 | } 8 | render () { 9 | return super.render(); 10 | } 11 | } -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /src/Components/Time.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import {Consumer} from '../ReStated'; 3 | 4 | const Time = () => { 5 | return ( 6 | 7 | {({state}) => ( 8 | {state.time.toString()} 9 | )} 10 | 11 | ); 12 | } 13 | 14 | export default Time; -------------------------------------------------------------------------------- /src/Components/TaskApp.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import TaskForm from './TaskForm'; 3 | import TaskList from './TaskList'; 4 | 5 | const TaskApp = () => ( 6 | 7 | 8 |
    9 | 10 |
11 |
12 | ) 13 | export default TaskApp; 14 | 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env.local 15 | .env.development.local 16 | .env.test.local 17 | .env.production.local 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | -------------------------------------------------------------------------------- /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 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .container { 2 | display: flex; 3 | flex-direction: column; 4 | align-items: center; 5 | justify-content: center; 6 | background-color: #FAFAFA; 7 | } 8 | 9 | .button-add { 10 | display: inline; 11 | font-size: 1.2em; 12 | } 13 | 14 | .todo-delete-button { 15 | margin-left: 20px; 16 | } 17 | 18 | .input-title { 19 | font-size: 1.5em; 20 | width: 350px; 21 | } 22 | 23 | .task-list { 24 | border: 1px solid gray; 25 | padding:15px; 26 | background-color: rgb(226, 224, 224); 27 | width: 80%; 28 | } 29 | 30 | .task-list li { 31 | padding-left: 0; 32 | margin: 15px; 33 | } -------------------------------------------------------------------------------- /src/Components/Notification.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from 'react'; 2 | 3 | export default class Notification extends Component{ 4 | constructor(props) { 5 | super(props); 6 | } 7 | 8 | shouldComponentUpdate(nextProps, nextState) { 9 | return(this.props.children !== nextProps.children); 10 | } 11 | componentDidMount() { 12 | console.log(this.props.context.state.notifications); 13 | } 14 | render() { 15 | console.log("Notifications->render"); 16 | return ( 17 |

{this.props.children}

18 | ); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-restated", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.3.0", 7 | "react-dom": "^16.3.0", 8 | "react-scripts": "^3.2.0" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test --env=jsdom", 14 | "eject": "react-scripts eject" 15 | }, 16 | "browserslist": { 17 | "production": [ 18 | ">0.2%", 19 | "not dead", 20 | "not op_mini all" 21 | ], 22 | "development": [ 23 | "last 1 chrome version", 24 | "last 1 firefox version", 25 | "last 1 safari version" 26 | ] 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Components/TaskForm.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import {Consumer } from '../ReStated'; 3 | 4 | const TaskForm = () => { 5 | let taskTitle = React.createRef(); 6 | return 7 | {(context) => ( 8 |
9 | {this.taskTitle = title}} 10 | type="text" placeholder="what do you want to do today?" /> 11 | 15 |
16 | )} 17 |
18 | } 19 | 20 | export default TaskForm; 21 | -------------------------------------------------------------------------------- /src/ReStated/index.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | 3 | // Create a context 4 | export const StateContext = React.createContext(null); 5 | export const {Provider, Consumer} = StateContext; 6 | 7 | export class Container extends Component { 8 | constructor() { 9 | super(); 10 | this.setup = this.setup.bind(this); 11 | } 12 | 13 | setup() { 14 | let map = {}; 15 | map.state = this.state; 16 | map.actions = this.actions; 17 | return map; 18 | } 19 | render () { 20 | let map = this.setup(); 21 | return ( 22 | 23 | {this.props.children} 24 | 25 | ) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Components/TaskList.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import {Consumer} from '../ReStated'; 3 | 4 | const TaskList = () => { 5 | const renderUI = (context) => { 6 | console.log("TaskList->render"); 7 | return context.state.tasks.map((task) => { 8 | return ( 9 |
  • 10 | {task.title} 11 | 15 |
  • 16 | ) 17 | }) 18 | } 19 | return ( 20 | 21 | {(context) => ( 22 | renderUI(context) 23 | )} 24 | 25 | ) 26 | } 27 | 28 | export default TaskList; -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
    29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import './App.css'; 3 | import {Consumer, Container} from './ReStated'; 4 | import TaskApp from './Components/TaskApp'; 5 | import Notification from './Components/Notification'; 6 | import TimeProvider from './Providers/TimeProvider'; 7 | import Time from './Components/Time'; 8 | 9 | // Create a Provider 10 | class MyProvider extends Container { 11 | state = { 12 | tasks: [ 13 | {id: 1, title: "New React Context API"}, 14 | {id: 2, title: "Learn VueJS"}, 15 | {id: 3, title: "Master NodeJS"}, 16 | ], 17 | notifications: [ 18 | {taskId: 1, message: "Message 1 TaskID 1"}, 19 | {taskId: 1, message: "Message 2 TaskID 1"}, 20 | {taskId: 2, message: "Message 1 for TaskID 2"}, 21 | 22 | ] 23 | } 24 | 25 | actions = { 26 | onAddTask: (title) => { 27 | console.log("adding..."); 28 | let maxId = Math.max.apply(Math, 29 | this.state.tasks.map((task)=>{return task.id})); 30 | 31 | let task = { 32 | id: maxId + 1 , 33 | title: title 34 | } 35 | 36 | this.setState({ 37 | tasks: [task, ...this.state.tasks] 38 | }) 39 | }, 40 | 41 | onDeleteTask: (taskId) => { 42 | console.log("onDeleteTask..."); 43 | let tasks = this.state.tasks.filter((task) => { 44 | return task.id !== taskId 45 | }) 46 | 47 | this.setState({ 48 | tasks 49 | }, ()=> { 50 | console.log("after update: ",this.state.tasks); 51 | }); 52 | } 53 | } 54 | 55 | render () { 56 | console.log("About to call parent render.."); 57 | return super.render(); 58 | } 59 | } 60 | 61 | class App extends Component { 62 | render() { 63 | return ( 64 | 65 |
    66 |

    Task Management App

    67 | 68 | 69 | {(context) => ( 70 | 71 | )} 72 | 73 |
    74 | 75 | 77 |
    78 | ); 79 | } 80 | } 81 | 82 | export default App; 83 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | 39 | // Add some additional logging to localhost, pointing developers to the 40 | // service worker/PWA documentation. 41 | navigator.serviceWorker.ready.then(() => { 42 | console.log( 43 | 'This web app is being served cache-first by a service ' + 44 | 'worker. To learn more, visit https://goo.gl/SC7cgQ' 45 | ); 46 | }); 47 | } else { 48 | // Is not local host. Just register service worker 49 | registerValidSW(swUrl); 50 | } 51 | }); 52 | } 53 | } 54 | 55 | function registerValidSW(swUrl) { 56 | navigator.serviceWorker 57 | .register(swUrl) 58 | .then(registration => { 59 | registration.onupdatefound = () => { 60 | const installingWorker = registration.installing; 61 | installingWorker.onstatechange = () => { 62 | if (installingWorker.state === 'installed') { 63 | if (navigator.serviceWorker.controller) { 64 | // At this point, the old content will have been purged and 65 | // the fresh content will have been added to the cache. 66 | // It's the perfect time to display a "New content is 67 | // available; please refresh." message in your web app. 68 | console.log('New content is available; please refresh.'); 69 | } else { 70 | // At this point, everything has been precached. 71 | // It's the perfect time to display a 72 | // "Content is cached for offline use." message. 73 | console.log('Content is cached for offline use.'); 74 | } 75 | } 76 | }; 77 | }; 78 | }) 79 | .catch(error => { 80 | console.error('Error during service worker registration:', error); 81 | }); 82 | } 83 | 84 | function checkValidServiceWorker(swUrl) { 85 | // Check if the service worker can be found. If it can't reload the page. 86 | fetch(swUrl) 87 | .then(response => { 88 | // Ensure service worker exists, and that we really are getting a JS file. 89 | if ( 90 | response.status === 404 || 91 | response.headers.get('content-type').indexOf('javascript') === -1 92 | ) { 93 | // No service worker found. Probably a different app. Reload the page. 94 | navigator.serviceWorker.ready.then(registration => { 95 | registration.unregister().then(() => { 96 | window.location.reload(); 97 | }); 98 | }); 99 | } else { 100 | // Service worker found. Proceed as normal. 101 | registerValidSW(swUrl); 102 | } 103 | }) 104 | .catch(() => { 105 | console.log( 106 | 'No internet connection found. App is running in offline mode.' 107 | ); 108 | }); 109 | } 110 | 111 | export function unregister() { 112 | if ('serviceWorker' in navigator) { 113 | navigator.serviceWorker.ready.then(registration => { 114 | registration.unregister(); 115 | }); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | The project is inspired from react-unstated and couple of other articles.(https://github.com/jamiebuilds/unstated). It is only intended for educational purpose as of now. 2 | 3 | ## Table of Contents 4 | 5 | - [Introduction to reStated](#intro-to-restated) 6 | - [Installation](#installation) 7 | - [Usage Examples](#usage-examples) 8 | - [Source Code](#source-code) 9 | - [Sending Feedback](#sending-feedback) 10 | 11 | ## Introduction to reStated 12 | 13 | ReStated is a state management library using more of an object oriented approach. 14 | 15 | ## Installation 16 | Right now the entire source code lives withing the below folder 17 | (https://github.com/rajeshpillai/react-restated/tree/master/src/ReStated/index.js) 18 | 19 | I will create an NPM package once I test this enough as I don't want to 20 | pollute the npm repository. 21 | 22 | ## Usage Examples 23 | 24 | The library is very simple to use. First import Consumer and Container 25 | in your main application. The below example is of a simple task/todo 26 | app wherein we demonstrate how to do the basic CRUD part of the app. 27 | 28 | ```js 29 | import {Consumer, Container} from './ReStated'; 30 | ``` 31 | 32 | Create a Provider. The provider should extends from the Container class. 33 | The provider exposes state and the actions. By default all actions defined 34 | here is available to the consumer/subscriber. 35 | 36 | In case you don't want any specific action to be available just begin the 37 | name of the action/method with an underscore,'_' 38 | ```js 39 | 40 | class MyProvider extends Container { 41 | state = { 42 | tasks: [ 43 | {id: 1, title: "New React Context API"}, 44 | {id: 2, title: "Learn VueJS"}, 45 | {id: 3, title: "Master NodeJS"}, 46 | ], 47 | notifications: [ 48 | {taskId: 1, message: "Message 1 TaskID 1"}, 49 | {taskId: 1, message: "Message 2 TaskID 1"}, 50 | {taskId: 2, message: "Message 1 for TaskID 2"}, 51 | 52 | ] 53 | } 54 | 55 | actions = { 56 | onAddTask: (title) => { 57 | console.log("adding..."); 58 | let maxId = Math.max.apply(Math, 59 | this.state.tasks.map((task)=>{return task.id})); 60 | 61 | let task = { 62 | id: maxId + 1 , 63 | title: title 64 | } 65 | 66 | this.setState({ 67 | tasks: [task, ...this.state.tasks] 68 | }) 69 | }, 70 | 71 | onDeleteTask: (taskId) => { 72 | console.log("onDeleteTask..."); 73 | let tasks = this.state.tasks.filter((task) => { 74 | return task.id !== taskId 75 | }) 76 | 77 | this.setState({ 78 | tasks 79 | }, ()=> { 80 | console.log("after update: ",this.state.tasks); 81 | }); 82 | } 83 | } 84 | 85 | render () { 86 | console.log("About to call parent render.."); 87 | return super.render(); 88 | } 89 | } 90 | ``` 91 | All state/data should go into the state property of the Provider class. And all actions should go into the action object. 92 | 93 | In the render() of the provider ensure to call the render of the base class 94 | by calling super.render(); 95 | 96 | Now your main App class can be coded as below. 97 | 98 | ```js 99 | class App extends Component { 100 | render() { 101 | return ( 102 | 103 |
    104 |

    Task Management App

    105 | 106 |
    107 |
    108 | ); 109 | } 110 | } 111 | ``` 112 | 113 | Please note, in whichever component you need state, wrap the component 114 | within the Consumer. 115 | 116 | Let's take a look at the TaskApp component. 117 | 118 | ```js 119 | import React from 'react'; 120 | import TaskForm from './TaskForm'; 121 | import TaskList from './TaskList'; 122 | 123 | const TaskApp = () => ( 124 | 125 | 126 |
      127 | 128 |
    129 |
    130 | ) 131 | export default TaskApp; 132 | 133 | 134 | ``` 135 | 136 | The TaskApp component as such doesn't need any state information. 137 | 138 | Now let's have a look at TaskForm and TaskList component. The TaskForm 139 | component needs access to the context as it has to invoke the actions 140 | on the provider. So, import the {Consumer} from the library and wrap 141 | your component within the component. The context is available 142 | as part of a render props function. 143 | 144 | ```js 145 | import React from 'react'; 146 | import {Consumer } from '../ReStated'; 147 | 148 | const TaskForm = () => ( 149 | 150 | {(context) => ( 151 |
    152 | {this.taskTitle = title}} 153 | type="text" placeholder="what do you want to do today?" /> 154 | 158 |
    159 | )} 160 |
    161 | ) 162 | 163 | export default TaskForm; 164 | ``` 165 | 166 | The TaskForm needs the context, as it has to invoke the onAddTask method when the add button is clicked. 167 | 168 | The below is the code for the TaskList component. The TaskList component 169 | also needs the context as it needs both the state info as well as the 170 | actions. 171 | 172 | ```js 173 | import React from 'react'; 174 | import {Consumer} from '../ReStated'; 175 | 176 | const TaskList = () => { 177 | const renderUI = (context) => { 178 | return context.state.tasks.map((task) => { 179 | return ( 180 |
  • 181 | {task.title} 182 | 186 |
  • 187 | ) 188 | }) 189 | } 190 | return ( 191 | 192 | {(context) => ( 193 | renderUI(context) 194 | )} 195 | 196 | ) 197 | } 198 | 199 | export default TaskList; 200 | 201 | ``` 202 | We can also create multiple providers if need be. For e.g. let's create 203 | a TimeProvider which supplies time and also a Time component that needs data 204 | from the TimeProvider. 205 | 206 | The below is the code for TimeProvider. Just extend any class from the 207 | Component and it has all the features required to become a Provider. 208 | 209 | ```js 210 | import React from 'react'; 211 | import {Container} from '../ReStated'; 212 | 213 | export default class TimeProvider extends Container { 214 | state = { 215 | time: new Date() 216 | } 217 | render () { 218 | return super.render(); 219 | } 220 | } 221 | ``` 222 | The TimeProvider above exposes only a state object with one attribute time. 223 | But you can use this as per your requirements add add methods, more properties etc. 224 | 225 | Now lets create a