Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Forget What You Know
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Christopher Pitt
September 22, 2016
Programming
210
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Forget What You Know
Just React things...
Christopher Pitt
September 22, 2016
Transcript
38 slides
FORGET WHAT YOU KNOW
WHAT I LEARNED ABOUT REACT THOUGH I MOSTLY DO SERVER-SIDE
AND A LITTLE BIT OF "WELL THAT WORKS WELL ENOUGH" JAVASCRIPT
None
const $issues = $(".issues") $.ajax({ "url": "https://api.github.com/repos/facebook/react/issues", "success": function(issues) {
issues.forEach(function(issue) { $issues.append(` <li class="issue"> <a class="title">${issue.title}</a> <div class="extract">${issue.body}</div> </li> `) }) } })
$issues.on("click", ".title", function(e) { const $title = $(this) $title.parent(".issue").toggleClass("highlight") $title.siblings(".extract").toggle()
})
None
"success": function(issues) { issues.forEach(function(issue) { $issues.append(` <li class="issue"> <a class="title">${issue.title}</a>
<a class="hide" data-id="${issue.id}">hide</a> <div class="extract">${issue.body}</div> </li> `) }) }
let hidden = [] $issues.on("click", ".hide", function(e) { const $hide
= $(this) const id = $hide.data("id") hidden.includes(id) || hidden.push(id) });
$.ajax({ "url": "https://api.github.com/repos/facebook/react/issues", "success": function(issues) { issues.forEach(function(issue) { $issues.append(`...`) })
} })
const render = function(issues) { $issues.empty() issues .filter(function(issue) { return
! hidden.includes(issue.id) }) .forEach(function(issue) { $issues.append(`...`) }) }
const fetch = function() { $.ajax({ "url": "https://api.github.com/repos/facebook/react/issues", "success": render
}) } fetch()
let hidden = [] try { hidden = JSON.parse(localStorage["hidden"]) }
catch (e) { console.warn("could not load hidden ids from local storage") }
$issues.on("click", ".hide", function(e) { const $hide = $(this) const id
= $hide.data("id") hidden.includes(id) || hidden.push(id) localStorage["hidden"] = JSON.stringify(hidden) fetch() });
OTHER THINGS WE COULD IMPROVE...
IMPERATIVE CODE ▸ make ajax request ▸ render list of
items ▸ do a thing on click ▸ persist ui state for refresh
IMPERATIVE CODE this is how to make things look like
I want
DECLARATIVE CODE this is what I want things to look
like given any state
<ul class="issues"> <li class="issue" ng-repeat="issue in issues" ng-if="visible"> <a class="title">{{
issue.title }}</a> <a class="hide" data-id="{{ issue.id }}">hide</a> <div class="extract">{{ issue.body }}</div> </li> </ul>
const Issues = ({ issues }) => { return (
<ul className="issues"> {issues.forEach((issue, key) => { if (!issue.visible) { return } return <Issue {...issue} key={key} /> }) </ul> ) }
class Issues extends React.Component { render() { return ( <ul
className="issues"> {this.props.issues.forEach((issue, key) => { if (!issue.visible) { return } return <Issue {...issue} key={key} /> }) </ul> ) } }
WHY IS DECLARATIVE CODE SOMETIMES BETTER?
REACT IS SCARY
USE FUNCTIONS INSTEAD OF CLASSES WHERE POSSIBLE
class Issues extends React.Component { componentWillMount() { // do something
before the component mounts } componentWillReceiveProps() { // do something after the component mounts } shouldComponentUpdate() { // return false if the component shouldn't re-render } }
class Issues extends React.Component { constructor(...params) { super(...params) this.state =
{ "text": "...list issues", } } async componentDidMount() { const response = await fetch("http://codepen.io/assertchris/pen/rrjKPN.css") const text = await response.text() this.setState({ ...this.state, "length": text.length, }) } render() { if (this.state.length) { return <span>{ this.state.text } ! { this.state.length }</span> } return <span>{ this.state.text }</span> } }
USE IMMUTABLE DATA WHERE POSSIBLE
this.setState({ ...this.state, "length": text.length, }) return [ ...items, "new item",
]
let state1 = Immutable.Map({ "text": "...list items", "length": 0, })
let state2 = map1.set("length", 43) state1.get("length") // 0 state2.get("length") // 43 state1.equals(state2) // false
https://facebook.github.io/immutable-js
YOU DON'T ALWAYS NEED FLUX OR REDUX OR REFLUX...
https://medium.com/@dan_abramov/ you-might-not-need-redux-be46360cf367
USE SERVICE LOCATION FOR PLUGIN ARCHITECTURE
// ...the code you write ! import { Ioc }
from "adonis-fold" import { hiddenReducer, highlightedReducer } from "path/to/core" Ioc.bind("reducers", function() { return [ hiddenReducer, highlightedReducer, ] })
// ...the code others write ! import { Ioc }
from "adonis-fold" import { pluginReducer } from "path/to/plugin" const previous = Ioc.use("reducers") Ioc.bind("reducers", function() { return [ ...previous, pluginReducer, ] })
const Issues = (props) => { const globals = Ioc.use("global-issues")
if (globals.length) { return ( <ul className="Issues"> { renderGlobalIssues(globals) } { renderIssues(props.issues) } </ul> ) } return ( <ul className="Issues"> { renderIssues(props.issues) } </ul> ) }
http://adonisjs.com/docs/3.0/overview#ioc-container
https://www.amazon.com/dp/B01BSTEDJ0
Thanks https://speakerdeck.com/chrispitt/forget-what-you-know @assertchris
More Decks by Christopher Pitt
See All by Christopher Pitt
Making Robots (PHP Unicorn Conf)
chrispitt
1
260
Transforming Magento (NomadMage 2017)
chrispitt
2
150
Monads In PHP → php[tek]
chrispitt
3
600
Breaking The Enigma → php[tek]
chrispitt
0
300
Turn on the Generator!
chrispitt
0
210
Implementing Languages (FluentConf)
chrispitt
1
400
Async PHP (Sunshine)
chrispitt
0
550
Helpful Robot
chrispitt
0
190
Async PHP
chrispitt
14
7.5k
Other Decks in Programming
See All in Programming
[DroidKnights 2026] Workflow AX 적용기 — 자동 정책 정리부터 E2E까지
dayeon
0
120
Apache Airflow Introduction
leew
1
100
例外からRich Errorsまで Kotlinエラーハンドリングの進化を読み解く
shiita0903
0
250
Promiseの向こう側へコンテキストを届ける ― proposal-async-context
seriseri
0
210
Getting Started with Vibe Coding
miccheng
0
250
Jujutsu で毎日のバージョン管理に「安心」と「直感性」を
oukayuka
0
100
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
220
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
170
ビジネスロジックを堅牢に~Scala 3のドメインプリミティブ~
j5ik2o
0
230
imagewebp()の向こう側: WebPはなぜ JPEG・PNGより軽いのか
yuzneri
0
150
エージェントの管理もエージェントに – herdr-core と自作バックログで作る司令塔 –
nearme_tech
PRO
2
150
Math.sumPreciseから学ぶ、ECMAScript仕様の数値の種類
syumai
0
340
Featured
See All Featured
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
580
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
61k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Building the Perfect Custom Keyboard
takai
2
970
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
480
How Software Deployment tools have changed in the past 20 years
geshan
3
35k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
540
Visualization
eitanlees
153
17k
Scaling GitHub
holman
464
140k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
320
Game over? The fight for quality and originality in the time of robots
wayneb77
1
340
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
1
250