Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Forget What You Know
Search
Christopher Pitt
September 22, 2016
Programming
180
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
More Decks by Christopher Pitt
See All by Christopher Pitt
Making Robots (PHP Unicorn Conf)
chrispitt
1
240
Transforming Magento (NomadMage 2017)
chrispitt
2
140
Monads In PHP → php[tek]
chrispitt
3
580
Breaking The Enigma → php[tek]
chrispitt
0
270
Turn on the Generator!
chrispitt
0
190
Implementing Languages (FluentConf)
chrispitt
1
380
Async PHP (Sunshine)
chrispitt
0
530
Helpful Robot
chrispitt
0
170
Async PHP
chrispitt
14
7.5k
Other Decks in Programming
See All in Programming
AIエージェントで 変わるAndroid開発環境
takahirom
1
140
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
150
Agentic UI
manfredsteyer
PRO
0
220
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
220
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
160
AI 輔助遺留系統現代化的經驗分享
jame2408
1
1.1k
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4.8k
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
130
The NotImplementedError Problem in Ruby
koic
1
1.1k
吝嗇家のためのAI活用 / AI development for miser - ChatGPT + Issue Driven Development
tooppoo
0
160
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
240
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
230
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Optimising Largest Contentful Paint
csswizardry
37
3.8k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
880
It's Worth the Effort
3n
188
29k
Done Done
chrislema
186
16k
A designer walks into a library…
pauljervisheath
211
24k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
380
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Test your architecture with Archunit
thirion
1
2.3k
Documentation Writing (for coders)
carmenintech
77
5.4k
Transcript
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