SlideShare a Scribd company logo
React: Rethinking Best Practices
Pete Hunt
@floydophone
Rethinking Best Practices
Give it five minutes.
http://37signals.com/svn/posts/3124-give-it-five-
minutes
Meet React.
A library for creating user
interfaces.
Meet React.
Renders your UI and responds to
events.
Meet React.
AKA: The V in MVC
Meet React.
Plays nicely with your stack,
whatever it may be.
Rethinking Best Practices
• Prerequisite
– Combine DOM generation and display logic
• React’s design
– Re-render the whole app on every update
• React’s implementation
– Virtual DOM and synthetic events
1. Build components, not
templates.
Rethinking Best Practices
We all like separation of
concerns, right?
Separation of concerns:
Reduce coupling, increase cohesion.
Coupling is:
“The degree to which each program
module relies on each of the other
modules.”
http://en.wikipedia.org/wiki/Coupling_(computer_science)
Cohesion is:
“The degree to which elements of a
module belong together.”
http://en.wikipedia.org/wiki/Cohesion_(computer_science)
Templates encourage a poor
separation of concerns.
So are Angular-style directives.
“View model” tightly couples
template to display logic.
[{“price”: “7.99”, “product”: “Back
scratcher”, “tableRowColor”:
“rgba(0, 0, 0, 0.5)”}]
Display logic and markup are
inevitably tightly coupled.
How do you find DOM nodes?
Display logic and markup are
highly cohesive.
They both show the UI.
Templates separate
technologies, not concerns.
And they do it by being deliberately
underpowered.
Symptoms that your front-end
technology is underpowered:
Reliance on primitive abstractions
(like {{> }} and {{#each }}).
Symptoms that your front-end
technology is underpowered:
Inventing lots of new concepts
(that already exist in JavaScript).
From the Angular directives docs:
“However isolated scope creates a new
problem: if a transcluded DOM is a child of
the widget isolated scope then it will not be
able to bind to anything. For this reason the
transcluded scope is a child of the original
scope, before the widget created an isolated
scope for its local variables. This makes the
transcluded and widget isolated scope
siblings.”
http://docs.angularjs.org/guide/directive
The framework cannot know how
to separate your concerns for you.
It should only provide powerful,
expressive tools for the user to do it
correctly.
This tool is a React component.
A highly cohesive building block for
UIs loosely coupled with other
components.
Use components to separate
your concerns.
With the full power of
JavaScript, not a crippled
templating language.
Abstraction
Composition
Expressivity
Components are reusable.
Components are composable.
Components are unit testable.
They are, after all, units.
What about spaghetti code?
Just don’t write spaghetti code.
Keep your components small.
Just don’t write spaghetti code.
Only put display logic in your
components.
Just don’t write spaghetti code.
“With great power comes great
responsibility” – Uncle Ben in
Spiderman
What about XSS?
React.DOM.a(
{href: „http://instagram.com/floydophone‟},
„@floydophone on Instagram‟
);
What about working with
designers?
JSX is an optional preprocessor
to let you use HTML-like syntax.
<a href=“http://instagram.com/floydophone”>
@floydophone on Instagram
</a>
JSX is an optional preprocessor
to let you use HTML-like syntax.
React.DOM.a(
{href: „http://instagram.com/floydophone‟},
„@floydophone on Instagram‟
)
With JSX, it’s easy for designers
to contribute code.
The accessibility of templates
and the power of JavaScript.
2. Re-render the whole app on
every update
The key design decision that makes
React awesome.
Building UIs is hard because
there is so much state.
Lots of UI elements · Design
iteration · Crazy environments ·
Mutable DOM · User input · etc etc
Data changing over time is the
root of all evil.
“Our intellectual powers are rather geared to
master static relations and our powers to visualize
processes evolving in time are relatively poorly
developed. For that reason we should do (as wise
programmers aware of our limitations) our utmost
to shorten the conceptual gap between the static
program and the dynamic process, to make the
correspondence between the program (spread
out in text space) and the process (spread out in
time) as trivial as possible” – Dijkstra
In the 90s it was easier.
Just refresh the page when the data
changes.
When the data changes, React re-
renders the entire component.
That is, React components are
basically just idempotent functions.
They describe your UI at any point in
time, just like a server-rendered
app.
Rethinking Best Practices
Re-rendering on every change
makes things simple.
Every place data is displayed is
guaranteed to be up-to-date.
Re-rendering on every change
makes things simple.
No magical data binding.
Re-rendering on every change
makes things simple.
No model dirty checking.
Re-rendering on every change
makes things simple.
No more explicit DOM operations –
everything is declarative.
“Re-render on every change?
That seems expensive.”
“And doesn’t it mess up form fields
and scroll position?”
3. Virtual DOM
Makes re-rendering on every
change fast.
You can’t just throw out the DOM
and rebuild it on each update.
It’s too slow and you’ll lose form
state and scroll position.
So we built a virtual DOM (and
events system).
Optimized for performance and
memory footprint
On every update…
• React builds a new virtual DOM subtree
• …diffs it with the old one
• …computes the minimal set of DOM
mutations and puts them in a queue
• …and batch executes all updates
React’s architecture looks a lot
like the Doom 3 engine
http://fabiensanglard.net/doom3/renderer.php
Game state /
input
Game logic Scene IR
Render
OpenGL ops
GFX card
http://fabiensanglard.net/doom3/renderer.php
App state /
events
React
components
Virtual
DOM
Compute DOM
operations
Browser
http://fabiensanglard.net/doom3/renderer.php
It’s fast!
Because the DOM is slow!
It’s fast!
Computes minimal DOM operations
It’s fast!
Batched reads and writes for
optimal DOM performance
It’s fast!
Usually faster than manual DOM
operations
It’s fast!
Automatic top-level event
delegation (with cross-browser
HTML5 events)
It’s fast!
Provides hooks for custom update
logic (though they’re almost never
used)
It’s fast!
Can do all this at 60fps, even in a
(non-JIT) UIWebView on the
iPhone.
The virtual DOM lets us do fun
things.
It can run in Node.js (new in 0.4)
The virtual DOM lets us do fun
things.
Optimizations based on app
structure
The virtual DOM lets us do fun
things.
Testability for free
The virtual DOM lets us do fun
things.
SVG, VML and <canvas> support
The virtual DOM lets us do fun
things.
Running the whole app in a Web
Worker (experimental)
Key takeaways
Components, not templates.
Re-render, don’t mutate.
Virtual DOM is simple and fast
Thanks for Rethinking Best
Practices with me!
http://reactjs.org/
#reactjs on Freenode IRC
reactjs on Google Groups

More Related Content

PPTX
Introduction to React
PPTX
reactJS
PPTX
PPTX
React web development
PDF
WEB DEVELOPMENT USING REACT JS
PPTX
React js - The Core Concepts
PDF
Introduction to ReactJS
PPTX
React JS: A Secret Preview
Introduction to React
reactJS
React web development
WEB DEVELOPMENT USING REACT JS
React js - The Core Concepts
Introduction to ReactJS
React JS: A Secret Preview

What's hot (20)

PDF
Best Practice-React
PDF
An Introduction to ReactJS
PPTX
Intro to React
PPTX
Reactjs
PPTX
A Brief Introduction to React.js
PDF
Introduction to React JS
ODP
Introduction to ReactJS
PPTX
React + Redux Introduction
PDF
React js
PDF
An introduction to React.js
PPTX
Its time to React.js
PPTX
React JS - A quick introduction tutorial
PDF
React Js Simplified
PPTX
Introduction to React JS
PDF
Angular 16 – the rise of Signals
PPTX
React workshop
PDF
Introduction To Single Page Application
PDF
Angular
PDF
Documenting your REST API with Swagger - JOIN 2014
PDF
TypeScript: coding JavaScript without the pain
Best Practice-React
An Introduction to ReactJS
Intro to React
Reactjs
A Brief Introduction to React.js
Introduction to React JS
Introduction to ReactJS
React + Redux Introduction
React js
An introduction to React.js
Its time to React.js
React JS - A quick introduction tutorial
React Js Simplified
Introduction to React JS
Angular 16 – the rise of Signals
React workshop
Introduction To Single Page Application
Angular
Documenting your REST API with Swagger - JOIN 2014
TypeScript: coding JavaScript without the pain
Ad

Similar to Rethinking Best Practices (20)

PPTX
React.js at Cortex
PDF
Tech Talk on ReactJS
PDF
ReactJS
PPTX
ReactJS Code Impact
PDF
Robust web apps with React.js
PDF
Getting Started with React, When You’re an Angular Developer
PDF
Learning React - I
PPTX
Reactjs notes.pptx for web development- tutorial and theory
PDF
React & Flux Workshop
PPTX
React js
PPTX
Introduction to ReactJS UI Web Dev .pptx
PPTX
ReactJS - Re-rendering pages in the age of the mutable DOM
PPTX
Introduction to React JS.pptx
PPT
React js
PDF
React DOM/Virtual DOM
PDF
React Fundamentals - Jakarta JS, Apr 2016
PPTX
Reactjs A javascript Library
PDF
React JS and why it's awesome
PDF
FRONTEND DEVELOPMENT WITH REACT.JS
PPSX
React introduction
React.js at Cortex
Tech Talk on ReactJS
ReactJS
ReactJS Code Impact
Robust web apps with React.js
Getting Started with React, When You’re an Angular Developer
Learning React - I
Reactjs notes.pptx for web development- tutorial and theory
React & Flux Workshop
React js
Introduction to ReactJS UI Web Dev .pptx
ReactJS - Re-rendering pages in the age of the mutable DOM
Introduction to React JS.pptx
React js
React DOM/Virtual DOM
React Fundamentals - Jakarta JS, Apr 2016
Reactjs A javascript Library
React JS and why it's awesome
FRONTEND DEVELOPMENT WITH REACT.JS
React introduction
Ad

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Big Data Technologies - Introduction.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Advanced IT Governance
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KodekX | Application Modernization Development
Cloud computing and distributed systems.
NewMind AI Monthly Chronicles - July 2025
Big Data Technologies - Introduction.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
GamePlan Trading System Review: Professional Trader's Honest Take
Mobile App Security Testing_ A Comprehensive Guide.pdf
Understanding_Digital_Forensics_Presentation.pptx
Machine learning based COVID-19 study performance prediction
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Advanced IT Governance
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Approach and Philosophy of On baking technology
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Rethinking Best Practices

Editor's Notes

  • #2: No fb
  • #3: SarcasticNot a tutorial
  • #10: Or: mixing markup and display logic
  • #11: Or: mixing markup and display logic
  • #21: PARTIALS
  • #24: Jab ember?
  • #27: Dismiss syntax
  • #31: “You want me to generate my markup in JavaScript!?”
  • #46: ----- Meeting Notes (8/22/13 16:30) -----which places in the page do i have to update my like count
  • #48: ----- Meeting Notes (8/22/13 16:30) -----easy to unit test
  • #49: ----- Meeting Notes (9/9/13 12:00) -----nowhere in this example is the dom node for count explicitly updated
  • #68: Two-way data binding talk