SlideShare a Scribd company logo
The 4 Layers of Single Page Applications
You Need to Know
Daniel Dughilă
Senior Software Development Engineer
The 4 Layers of Single Page Applications You Need to Know
3 Orange Restricted
Big ball of mud architecture
Every successful project needs
a clear architecture, understood by all
team members
Let’s architect a React application
Our app will display a list
of articles
As a user, I will be able to
create, delete and like
articles
Today’s frameworks are declarative
React, Angular, Vue, encourage us to use elements of
functional programming
Have you ever seen a flip book?
Credits theflippist @youtube.com
“Design simple views for each state in your
application and React will efficiently update
and render just the right components when
your data changes …”
“Build features quickly with simple,
declarative templates …”
Frameworks help us build apps consisting of views
Views are representations of state
But what is the state?
The state represents every piece of data that changes in
an application
The state should be immutable
Credits James K Nelson
Immutability
“Immutability makes tracking changes cheap.
A change will always result in a new object so we
only need to check if the reference to the object
has changed”
Controversial in OOP, especially in large-scale
applications, the anemic domain model is
acceptable when working with immutable data.
The domain layer
Let’s model the Article entity
// @flow
export type Article = {
+id: string;
+likes: number;
+title: string;
+author: string;
}
Article service
export type ArticleFields = {
+title: string;
+author: string;
}
export const createArticle = (articleFields: ArticleFields): ?Article => {
const {title, author} = articleFields;
return isTitleValid(title) && isAuthorValid(author) ?
Object.freeze({
id: v1(), likes: 0, title, author
}) :
null;
};
export const updateLikes = (article: Article, likes: number) => ...
export const isTitleValid = (title: string) => ...
export const isAuthorValid = (author: string) => ...
Creating Articles, agnostic of a framework
import * as articleService from "../domain/ArticleService";
const article = articleService.createArticle({
title: '12 rules for life',
author: 'Jordan Peterson'
});
/*
const itWillPrint = {
id: "92832a9a-ec55-46d7-a34d-870d50f191df",
likes: 0,
title: "12 rules for life",
author: "Jordan Peterson"
};
*/
ArticleState
// @flow
import type {Article} from "./Article";
export type ArticleState = Article[];
The store layer
Credits Ahmed Shamim Hassan
ArticleStore
let articleState: ArticleState = Object.freeze([]);
let subscribers: Function[] = Object.freeze([]);
export const notify = (articleState: ArticleState, subscribers: Function[]) => ...
export const addArticle = (article: Article) => ...
export const removeArticle = (article: Article) => ...
export const updateArticle = (article: Article) => ...
export const subscribe = (subscriber: Function) => ...
export const unsubscribe = (subscriber: Function) => ...
Application services
For whatever reason,
a designer comes and
demands all author names
to be UPPERCASE
ArticleUi service
export const displayAuthor = (author: string) => author.toUpperCase();
export const fetchInitialArticles = (): Article[] => ...
View layer
App component
class App extends Component<Props> {
render() {
return (
<div>
<ArticleFormContainer/>
<ArticleListContainer/>
</div>
);
}
}
ArticleForm Container
ArticleForm Component
Store
ArticleForm component
Change
title
Change
author
Submit
form
export class ArticleFormContainer extends Component<Props, FormData> {
...
submitForm(event: Event) {
...
if (isTitleValid && isAuthorValid) {
const newArticle = articleService.createArticle({
title: articleTitle,
author: articleAuthor
});
if (newArticle) { articleStore.addArticle(newArticle); }
...
}
render() {
return (
<ArticleFormComponent
submitForm={this.submitForm.bind(this)}
...
ArticleForm container
ArticleList Container
ArticleList Component
Store
ArticleList container
export class ArticleListContainer extends React.Component<Props, State> {
...
constructor(props: Props) {
super(props);
this.state = {
articles: []
};
this.subscriber = articleStore.subscribe((articles: Article[]) => {
this.setState({articles});
});
}
...
render() {
return <ArticleListComponent {...this.state}/>;
}
}
ArticleList component
Article component
Delete action
Uppercase Projection
Like action
Article Container
Article Component
Store
export const ArticleComponent = (props: Props) => {
...
return (
<Card>
<CardBody>
<CardTitle>{article.title}</CardTitle>
<CardSubtitle>
{articleUiService.displayAuthor(article.author)}
</CardSubtitle>
<CardText>{article.likes}</CardText>
<Button onClick={() => likeArticle(article)}>Like</Button>{' '}
<Button onClick={() => deleteArticle(article)}>Delete</Button>
</CardBody>
</Card>
...
Article component
Article container
export class ArticleContainer extends Component<Props> {
likeArticle(article: Article) {
const updatedArticle = articleService.updateLikes(article, article.likes + 1);
articleStore.updateArticle(updatedArticle);
}
removeArticle(article: Article) {
articleStore.removeArticle(article);
}
render() {
return (
<ArticleComponent
article={this.props.article}
likeArticle={(article: Article) => this.likeArticle(article)}
deleteArticle={(article: Article) => this.removeArticle(article)}
/>
...
We now have a fully functional
React app and a robust, clear
defined architecture.
Anyone who joins our team
can feel comfortable to
continue our work.
Thank you
medium.com@danieldughila
@danielDughy
github.com/intojs

More Related Content

PDF
Who's afraid of front end databases
PDF
Practical Dynamic Actions - Intro
PDF
Introduction to react
PDF
React Fundamentals - Jakarta JS, Apr 2016
PPSX
React introduction
PPTX
Reactjs
PDF
PPTX
React JS: A Secret Preview
Who's afraid of front end databases
Practical Dynamic Actions - Intro
Introduction to react
React Fundamentals - Jakarta JS, Apr 2016
React introduction
Reactjs
React JS: A Secret Preview

What's hot (20)

PDF
Redux and context api with react native app introduction, use cases, implemen...
PDF
React js
PDF
Integrating consumers IoT devices into Business Workflow
PPTX
React js
PDF
JOSA TechTalks - Better Web Apps with React and Redux
PPTX
reactJS
PDF
React vs Angular2
PPTX
Reducers+flux=redux
PDF
Robust web apps with React.js
PPTX
VIPER Architecture
PPTX
React js Online Training
PPTX
React js
PPTX
React js Rahil Memon
PPTX
PDF
Introduction to ReactJS
PPTX
React js programming concept
PDF
Understanding Facebook's React.js
PPTX
Introduction to React
PDF
Breaking the Server-Client Divide with Node.js and React
PPT
Corespring
Redux and context api with react native app introduction, use cases, implemen...
React js
Integrating consumers IoT devices into Business Workflow
React js
JOSA TechTalks - Better Web Apps with React and Redux
reactJS
React vs Angular2
Reducers+flux=redux
Robust web apps with React.js
VIPER Architecture
React js Online Training
React js
React js Rahil Memon
Introduction to ReactJS
React js programming concept
Understanding Facebook's React.js
Introduction to React
Breaking the Server-Client Divide with Node.js and React
Corespring
Ad

Similar to The 4 Layers of Single Page Applications You Need to Know (20)

PDF
From Back to Front: Rails To React Family
PPTX
React & redux
PDF
Tokyo React.js #3: Missing Pages: ReactJS/Flux/GraphQL/RelayJS
PPTX
Thinking in react
PDF
React.js: You deserve to know about it
PPTX
Build the API you want to see in the world
PPTX
PDF
A Journey with React
PDF
An Intense Overview of the React Ecosystem
PDF
FRONTEND DEVELOPMENT WITH REACT.JS
PDF
The Road To Redux
PDF
Frontin like-a-backer
PPTX
Adding a modern twist to legacy web applications
PDF
[@NaukriEngineering] Flux Architecture
PDF
The road-to-learn-react
PDF
Use React Patterns to Build Large Scalable App
PDF
Michelle Garrett - Build the API you want to see in the world (with GraphQL) ...
PDF
Michelle Garrett - Build the API you want to see in the world (with GraphQL) ...
PPTX
theory-slides-for react for beginners.pptx
PPTX
Adding a modern twist to legacy web applications
From Back to Front: Rails To React Family
React & redux
Tokyo React.js #3: Missing Pages: ReactJS/Flux/GraphQL/RelayJS
Thinking in react
React.js: You deserve to know about it
Build the API you want to see in the world
A Journey with React
An Intense Overview of the React Ecosystem
FRONTEND DEVELOPMENT WITH REACT.JS
The Road To Redux
Frontin like-a-backer
Adding a modern twist to legacy web applications
[@NaukriEngineering] Flux Architecture
The road-to-learn-react
Use React Patterns to Build Large Scalable App
Michelle Garrett - Build the API you want to see in the world (with GraphQL) ...
Michelle Garrett - Build the API you want to see in the world (with GraphQL) ...
theory-slides-for react for beginners.pptx
Adding a modern twist to legacy web applications
Ad

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
System and Network Administration Chapter 2
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Nekopoi APK 2025 free lastest update
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
Transform Your Business with a Software ERP System
PPTX
history of c programming in notes for students .pptx
PPT
Introduction Database Management System for Course Database
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
System and Network Administraation Chapter 3
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Softaken Excel to vCard Converter Software.pdf
ManageIQ - Sprint 268 Review - Slide Deck
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Which alternative to Crystal Reports is best for small or large businesses.pdf
System and Network Administration Chapter 2
How to Migrate SBCGlobal Email to Yahoo Easily
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Navsoft: AI-Powered Business Solutions & Custom Software Development
Nekopoi APK 2025 free lastest update
Understanding Forklifts - TECH EHS Solution
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Transform Your Business with a Software ERP System
history of c programming in notes for students .pptx
Introduction Database Management System for Course Database
Upgrade and Innovation Strategies for SAP ERP Customers

The 4 Layers of Single Page Applications You Need to Know

  • 1. The 4 Layers of Single Page Applications You Need to Know Daniel Dughilă Senior Software Development Engineer
  • 3. 3 Orange Restricted Big ball of mud architecture
  • 4. Every successful project needs a clear architecture, understood by all team members
  • 5. Let’s architect a React application
  • 6. Our app will display a list of articles As a user, I will be able to create, delete and like articles
  • 7. Today’s frameworks are declarative React, Angular, Vue, encourage us to use elements of functional programming Have you ever seen a flip book?
  • 9. “Design simple views for each state in your application and React will efficiently update and render just the right components when your data changes …” “Build features quickly with simple, declarative templates …”
  • 10. Frameworks help us build apps consisting of views Views are representations of state But what is the state?
  • 11. The state represents every piece of data that changes in an application The state should be immutable Credits James K Nelson
  • 12. Immutability “Immutability makes tracking changes cheap. A change will always result in a new object so we only need to check if the reference to the object has changed” Controversial in OOP, especially in large-scale applications, the anemic domain model is acceptable when working with immutable data.
  • 14. Let’s model the Article entity // @flow export type Article = { +id: string; +likes: number; +title: string; +author: string; }
  • 15. Article service export type ArticleFields = { +title: string; +author: string; } export const createArticle = (articleFields: ArticleFields): ?Article => { const {title, author} = articleFields; return isTitleValid(title) && isAuthorValid(author) ? Object.freeze({ id: v1(), likes: 0, title, author }) : null; }; export const updateLikes = (article: Article, likes: number) => ... export const isTitleValid = (title: string) => ... export const isAuthorValid = (author: string) => ...
  • 16. Creating Articles, agnostic of a framework import * as articleService from "../domain/ArticleService"; const article = articleService.createArticle({ title: '12 rules for life', author: 'Jordan Peterson' }); /* const itWillPrint = { id: "92832a9a-ec55-46d7-a34d-870d50f191df", likes: 0, title: "12 rules for life", author: "Jordan Peterson" }; */
  • 17. ArticleState // @flow import type {Article} from "./Article"; export type ArticleState = Article[];
  • 20. ArticleStore let articleState: ArticleState = Object.freeze([]); let subscribers: Function[] = Object.freeze([]); export const notify = (articleState: ArticleState, subscribers: Function[]) => ... export const addArticle = (article: Article) => ... export const removeArticle = (article: Article) => ... export const updateArticle = (article: Article) => ... export const subscribe = (subscriber: Function) => ... export const unsubscribe = (subscriber: Function) => ...
  • 22. For whatever reason, a designer comes and demands all author names to be UPPERCASE
  • 23. ArticleUi service export const displayAuthor = (author: string) => author.toUpperCase(); export const fetchInitialArticles = (): Article[] => ...
  • 25. App component class App extends Component<Props> { render() { return ( <div> <ArticleFormContainer/> <ArticleListContainer/> </div> ); } }
  • 28. export class ArticleFormContainer extends Component<Props, FormData> { ... submitForm(event: Event) { ... if (isTitleValid && isAuthorValid) { const newArticle = articleService.createArticle({ title: articleTitle, author: articleAuthor }); if (newArticle) { articleStore.addArticle(newArticle); } ... } render() { return ( <ArticleFormComponent submitForm={this.submitForm.bind(this)} ... ArticleForm container
  • 30. ArticleList container export class ArticleListContainer extends React.Component<Props, State> { ... constructor(props: Props) { super(props); this.state = { articles: [] }; this.subscriber = articleStore.subscribe((articles: Article[]) => { this.setState({articles}); }); } ... render() { return <ArticleListComponent {...this.state}/>; } }
  • 34. export const ArticleComponent = (props: Props) => { ... return ( <Card> <CardBody> <CardTitle>{article.title}</CardTitle> <CardSubtitle> {articleUiService.displayAuthor(article.author)} </CardSubtitle> <CardText>{article.likes}</CardText> <Button onClick={() => likeArticle(article)}>Like</Button>{' '} <Button onClick={() => deleteArticle(article)}>Delete</Button> </CardBody> </Card> ... Article component
  • 35. Article container export class ArticleContainer extends Component<Props> { likeArticle(article: Article) { const updatedArticle = articleService.updateLikes(article, article.likes + 1); articleStore.updateArticle(updatedArticle); } removeArticle(article: Article) { articleStore.removeArticle(article); } render() { return ( <ArticleComponent article={this.props.article} likeArticle={(article: Article) => this.likeArticle(article)} deleteArticle={(article: Article) => this.removeArticle(article)} /> ...
  • 36. We now have a fully functional React app and a robust, clear defined architecture. Anyone who joins our team can feel comfortable to continue our work.