Skip to content

Repository files navigation

Universal Model for React

versionDownloadsbuildcoverageQuality Gate StatusMIT LicenseFOSSA Status

Universal model is a model which can be used with any of following UI frameworks:

If you want to use multiple UI frameworks at the same time, you can use single model with universal-model library

Install

npm install --save universal-model-react

Prerequisites for universal-model-react

"react": "^16.8.0"

Clean UI Architecture

alt text

  • Model-View-Controller (https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller)
  • User triggers actions by using view or controller
  • Actions are part of model and they manipulate state that is stored
  • Actions can use services to interact with external (backend) systems
  • State changes trigger view updates
  • Selectors select and calculate a transformed version of state that causes view updates
  • Views contain NO business logic
  • There can be multiple interchangeable views that use same part of model
  • A new view can be created to represent model differently without any changes to model
  • View technology can be changed without changes to the model

Clean UI Code directory layout

UI application is divided into UI components. Common UI components should be put into common directory. Each component can consist of subcomponents. Each component has a view and optionally controller and model. Model consists of actions, state and selectors. In large scale apps, model can contain sub-store. Application has one store which is composed of each components' state (or sub-stores)

- src
|
|- common
| |- component1
| |- component2
| . |- component2_1
| . | . | .
|- componentA
|- componentB
| |- componentB_1
| |- componentB_2
|- componentC
| |- view
| .
| .
|- componentN
| |- controller
| |- model
| | |- actions
| | |- services
| | |- state
| |- view
|- store

API

Common API (Angular/React/Svelte/Vue)

createSubState(subState);conststore=createStore(initialState,combineSelectors(selectors))const{ componentAState }=store.getState();const{ selector1, selector2 }=store.getSelectors();const[{ componentAState },{ selector1, selector2 }]=store.getStateAndSelectors();

React specifi API

useState([componentAState]);useSelectors([selector1,selector2]);useStateAndSelectors([componentAState],[selector1,selector2]);

Detailed API documentation

API Examples

Create initial states

constinitialComponentAState={prop1: 0,prop2: 0};

Create selectors

When using foreign state inside selectors, prefer creating foreign state selectors and accessing foreign state through them instead of directly accessing foreign state inside selector. This will ensure better encapsulation of component state. For example:

constcreateComponentASelectors=<TextendsState>()=>({selector1: (state: State)=>state.componentAState.prop1+state.componentAState.prop2selector2: (state: State)=>{const{ componentBSelector1, componentBSelector2 }=createComponentBSelectors<State>();returnstate.componentAState.prop1+componentBSelector1(state)+componentBSelector2(state);}});

Create and export store in store.ts:

combineSelectors() checks if there are duplicate keys in selectors and will throw an error telling which key was duplicated. By using combineSelectors you can keep your selector names short and only namespace them if needed.

constinitialState={componentAState: createSubState(initialComponentAState),componentBState: createSubState(initialComponentBState)};exporttypeState=typeofinitialState;constcomponentAStateSelectors=createComponentAStateSelectors<State>();constcomponentBStateSelectors=createComponentBStateSelectors<State>();constselectors=combineSelectors<State,typeofcomponentAStateSelectors,typeofcomponentBStateSelectors>(componentAStateSelectors,componentBStateSelectors);exportdefaultcreateStore<State,typeofselectors>(initialState,selectors);

in large projects you should have sub-stores for components and these sub-store are combined together to a single store in store.js:

componentBSubStore.js

constinitialComponentsBState={componentBState: createSubState(initialComponentBState),componentB_1State: createSubState(initialComponentB_1State),componentB_2State: createSubState(initialComponentB_2State)};constcomponentBStateSelectors=createComponentBStateSelectors<State>();constcomponentB_1StateSelectors=createComponentB_1StateSelectors<State>();constcomponentB_2StateSelectors=createComponentB_2StateSelectors<State>();constcomponentsBSelectors=combineSelectors<State,typeofcomponentBStateSelectors,typeofcomponentB_1StateSelectors,typeofcomponentB_2StateSelectors>(componentBStateSelectors,componentB_1StateSelectors,componentB_2StateSelectors);

store.js

constinitialState={
...initialComponentsAState,
...intialComponentsBState,.
...initialComponentsNState};exporttypeState=typeofinitialState;constselectors=combineSelectors<State,typeofcomponentsAStateSelectors,typeofcomponentsBStateSelectors, ... typeofcomponentsNStateSelectors>(componentsAStateSelectors,componentsBStateSelectors,.componentsNStateSelectors);exportdefaultcreateStore<State,typeofselectors>(initialState,selectors);

Access store in Actions

Don't modify other component's state directly inside action, but instead call other component's action. This will ensure encapsulation of component's own state.

exportdefaultfunctionchangeComponentAAndBState(newAValue,newBValue){const{ componentAState }=store.getState();componentAState.prop1=newAValue;// BADconst{ componentBState }=store.getState();componentBState.prop1=newBValue;// GOODchangeComponentBState(newBValue);}

Use actions, state and selectors in Views (React functional components)

Class-based components are not currently supported.

Components should use only their own state and access other components' states using selectors provided by those components. This will ensure encapsulation of each component's state. For example:

constView=()=>{const[{ componentAState },{ selector1, selector2 }]=store.getStateAndSelectors();store.useStateAndSelectors([componentAState],[selector1,selector2]);// NOTE! Get the value of a selector using it's 'value' property!console.log(selector1.value);}

It is also possible to access foreign state directly using a state getter function:

constView=()=>{const[{ componentAState, componentBState },{ selector1 }]=store.getStateAndSelectors();store.useStateAndSelectors([componentAState,()=>componentBState.property1],[selector1]);console.log(componentBState.property1);}

Example

View

App.tsx

import*asReactfrom'react';importHeaderViewfrom'@/header/view/HeaderView';importTodoListViewfrom'@/todolist/view/TodoListView';constApp=()=>(<div><HeaderView/><TodoListView/></div>);exportdefaultApp;

HeaderView.tsx

import*asReactfrom'react';importstorefrom'@/store/store';importchangeUserNamefrom'@/header/model/actions/changeUserName';constHeaderView=()=>{const{ headerText }=store.getSelectors();store.useSelectors([headerText]);return(<div><h1>{headerText.value}</h1><label>User name:</label><inputonChange={({target: { value }})=>changeUserName(value)}/></div>);};exportdefaultHeaderView;

TodoListView.tsx

import*asReactfrom'react';import{useEffect}from'react';importstorefrom'@/store/store';import{Todo}from'@/todolist/model/state/initialTodoListState';importremoveTodofrom'@/todolist/model/actions/removeTodo';importfetchTodosfrom'@/todolist/model/actions/fetchTodos';importtodoListControllerfrom'@/todolist/controller/todoListController';importtoggleIsDoneTodofrom'@/todolist/model/actions/toggleIsDoneTodo';importtoggleShouldShowOnlyUnDoneTodosfrom'@/todolist/model/actions/toggleShouldShowOnlyUnDoneTodos';constTodoListView=()=>{const[{ todosState },{ shownTodos, userName }]=store.getStateAndSelectors();store.useStateAndSelectors([todosState],[shownTodos,userName]);useEffect(()=>{// noinspection JSIgnoredPromiseFromCallfetchTodos();document.addEventListener('keydown',todoListController.handleKeyDown);return()=>document.removeEventListener('keydown',todoListController.handleKeyDown);},[]);lettodoListContent;if(todosState.isFetchingTodos){todoListContent=<div>Fetching todos...</div>;}elseif(todosState.hasTodosFetchFailure){todoListContent=<div>Failed to fetch todos</div>;}else{consttodoListItems=shownTodos.value.map((todo: Todo,index: number)=>(<likey={todo.id}><inputid={todo.name}type="checkbox"defaultChecked={todo.isDone}onChange={()=>toggleIsDoneTodo(todo)}/><label>{userName.value}: {todo.name}</label><buttononClick={()=>removeTodo(todo)}>Remove</button></li>));todoListContent=<ul>{todoListItems}</ul>;}return(<div><inputid="shouldShowOnlyDoneTodos"type="checkbox"defaultChecked={todosState.shouldShowOnlyUnDoneTodos}onChange={toggleShouldShowOnlyUnDoneTodos}/><label>Show only undone todos</label>{todoListContent}</div>);};exportdefaultTodoListView;

Controller

todoListController.ts

importaddTodofrom"@/todolist/model/actions/addTodo";importremoveAllTodosfrom"@/todolist/model/actions/removeAllTodos";exportdefault{handleKeyDown(keyboardEvent: KeyboardEvent): void{if(keyboardEvent.code==='KeyA'&&keyboardEvent.ctrlKey){keyboardEvent.stopPropagation();keyboardEvent.preventDefault();addTodo();}elseif(keyboardEvent.code==='KeyR'&&keyboardEvent.ctrlKey){keyboardEvent.stopPropagation();keyboardEvent.preventDefault();removeAllTodos();}}};

Model

Store

store.ts

import{combineSelectors,createStore,createSubState}from'universal-model-react';importinitialHeaderStatefrom'@/header/model/state/initialHeaderState';importinitialTodoListStatefrom'@/todolist/model/state/initialTodoListState';importcreateTodoListStateSelectorsfrom'@/todolist/model/state/createTodoListStateSelectors';importcreateHeaderStateSelectorsfrom'@/header/model/state/createHeaderStateSelectors';constinitialState={headerState: createSubState(initialHeaderState),todosState: createSubState(initialTodoListState)};exporttypeState=typeofinitialState;constheaderStateSelectors=createHeaderStateSelectors<State>();consttodoListStateSelectors=createTodoListStateSelectors<State>();constselectors=combineSelectors<State,typeofheaderStateSelectors,typeoftodoListStateSelectors>(headerStateSelectors,todoListStateSelectors);exportdefaultcreateStore<State,typeofselectors>(initialState,selectors);

State

Initial state

initialHeaderState.ts

exportdefault{userName: 'John'};

initialTodoListState.ts

exportinterfaceTodo{id: number,name: string;isDone: boolean;}exportdefault{todos: []asTodo[],shouldShowOnlyUnDoneTodos: false,isFetchingTodos: false,hasTodosFetchFailure: false};

State selectors

createHeaderStateStateSelectors.ts

import{State}from'@/store/store';constcreateHeaderStateSelectors=<TextendsState>()=>({userName: (state: T)=>state.headerState.userName,headerText: (state: T)=>{const{todoCount: selectTodoCount,unDoneTodoCount: selectUnDoneTodoCount}=createTodoListStateSelectors<T>();return`${state.headerState.userName} (${selectUnDoneTodoCount(state)}/${selectTodoCount(state)})`;}});exportdefaultcreateHeaderStateSelectors;

createTodoListStateSelectors.ts

import{State}from'@/store/store';import{Todo}from'@/todolist/model/state/initialTodoListState';constcreateTodoListStateSelectors=<TextendsState>()=>({shownTodos: (state: T)=>state.todosState.todos.filter((todo: Todo)=>(state.todosState.shouldShowOnlyUnDoneTodos&&!todo.isDone)||!state.todosState.shouldShowOnlyUnDoneTodos),todoCount: (state: T)=>state.todosState.todos.length,unDoneTodoCount: (state: T)=>state.todosState.todos.filter((todo: Todo)=>!todo.isDone).length});exportdefaultcreateTodoListStateSelectors;

Service

ITodoService.ts

import{Todo}from'@/todolist/model/state/initialTodoListState';exportinterfaceITodoService{tryFetchTodos(): Promise<Todo[]>;}

FakeTodoService.ts

import{ITodoService}from'@/todolist/model/service/ITodoService';import{Todo}from'@/todolist/model/state/initialTodoListState';importConstantsfrom'@/Constants';exportdefaultclassFakeTodoServiceimplementsITodoService{tryFetchTodos(): Promise<Todo[]>{returnnewPromise<Todo[]>((resolve: (todo: Todo[])=>void,reject: ()=>void)=>{setTimeout(()=>{if(Math.random()<0.95){resolve([{id: 1,name: 'first todo',isDone: true},{id: 2,name: 'second todo',isDone: false}]);}else{reject();}},Constants.FAKE_SERVICE_LATENCY_IN_MILLIS);});}}

todoService.ts

importFakeTodoServicefrom"@/todolist/model/services/FakeTodoService";exportdefaultnewFakeTodoService();

Actions

changeUserName.ts

importstorefrom'@/store/store';exportdefaultfunctionchangeUserName(newUserName: string): void{const{ headerState }=store.getState();headerState.userName=newUserName;}

addTodo.ts

importstorefrom'@/store/store';letid=3;exportdefaultfunctionaddTodo(): void{const{ todosState }=store.getState();todosState.todos.push({ id,name: 'new todo',isDone: false});id++;}

removeTodo.ts

importstorefrom'@/store/store';import{Todo}from'@/todolist/model/state/initialTodoListState';exportdefaultfunctionremoveTodo(todoToRemove: Todo): void{const{ todosState }=store.getState();todosState.todos=todosState.todos.filter((todo: Todo)=>todo!==todoToRemove);}

removeAllTodos.ts

importstorefrom'@/store/store';exportdefaultfunctionremoveAllTodos(): void{const{ todosState }=store.getState();todosState.todos=[];}

toggleIsDoneTodo.ts

import{Todo}from'@/todolist/model/state/initialTodoListState';exportdefaultfunctiontoggleIsDoneTodo(todo: Todo): void{todo.isDone=!todo.isDone;}

toggleShouldShowOnlyUnDoneTodos.ts

importstorefrom'@/store/store';exportdefaultfunctiontoggleShouldShowOnlyUnDoneTodos(): void{const{ todosState }=store.getState();todosState.shouldShowOnlyUnDoneTodos=!todosState.shouldShowOnlyUnDoneTodos;}

fetchTodos.ts

importstorefrom'@/store/store';importtodoServicefrom'@/todolist/model/services/todoService';exportdefaultasyncfunctionfetchTodos(): Promise<void>{const{ todosState }=store.getState();todosState.isFetchingTodos=true;todosState.hasTodosFetchFailure=false;try{todosState.todos=awaittodoService.tryFetchTodos();}catch(error){todosState.hasTodosFetchFailure=true;}todosState.isFetchingTodos=false;}

Full Examples

https://github.com/universal-model/universal-model-react-todo-app

https://github.com/universal-model/universal-model-react-todos-and-notes-app

Dependency injection

If you would like to use dependency injection (noicejs) in your app, check out this example, where DI is used to create services.

Donate/Sponsor

If you would like to help me to develop more great stuff, you can donate or sponsor. Thank you!

License

MIT License

Releases

Sponsor this project

Packages

Used by

Contributors

Languages