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
Arquitetura para a nova geração de aplicações
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Charbel
October 19, 2019
Programming
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Arquitetura para a nova geração de aplicações
Talk @ React Conf Brasil. Logic Hooks, Interfaces, Pure Components
Charbel
October 19, 2019
Other Decks in Programming
See All in Programming
APNsからLive Activityを開始する話
yumnumm
0
170
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
190
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
1.1k
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
35
17k
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
2
970
残高管理から台帳サービスへの進化
artoy
1
200
Next.js: The final boss of Web Performance
guaca
0
280
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
320
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
160
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
110
Featured
See All Featured
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
480
From π to Pie charts
rasagy
1
400
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.3k
Balancing Empowerment & Direction
lara
6
1.3k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
800
Building Adaptive Systems
keathley
44
3.3k
Un-Boring Meetings
codingconduct
0
460
RailsConf 2023
tenderlove
30
1.6k
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.2k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
How to train your dragon (web standard)
notwaldorf
96
6.9k
How to make the Groovebox
asonas
2
2.5k
Transcript
Arquitetura para a nova geração de aplicações com React e
React Native Charbel Rami @charbelrami HOOKS :)
Charbel Rami - Faço coisas com código desde os 13
(comecei com RGSS no RPG Maker); - Gosto de trabalhar com acessibilidade e performance; - Fui colaborador no início do Brave; - Trabalho na VAGAS.
Um dia normal em um chat qualquer:
Cliente: — Sabe o que seria legal? Front:
Cliente: ▶ ──────── 2:59:03 Front:
Cliente: — Resumindo: um Todo List para web e mobile
;) Front:
Front:
Front: if (React && React Native && React Native Web)
SUCESSO!
Como o Front imagina:
None
None
Como realmente é:
None
None
Front:
None
Hooks - São funções com suporte para as principais funcionalidades
do React, como state, lifecycle e context; - Não renderizam nada;
Hooks - Possuem estado local isolado no componente em que
se encontram; - Permitem reutilizar lógica de estado.
Coooode
export function Todos() { const [title, setTitle] = useState(""); const
todos = useContext(TodoStateContext); … useEffect(() => { … }, [todos, id]); function addTodo() { … } function handleTitleChange() { … } … return ( <> <input value={title} onChange={handleTitleChange} … /> … <button onClick={addTodo}>Add Todo</button> <ol> {todos.map(({ id, title }) => ( …
export function Todos() { const [title, setTitle] = useState(""); const
todos = useContext(TodoStateContext); … useEffect(() => { … }, [todos, id]); function addTodo() { … } function handleTitleChange() { … } … return ( <> <input value={title} onChange={handleTitleChange} … /> … <button onClick={addTodo}>Add Todo</button> <ol> {todos.map(({ id, title }) => ( …
// pure-components/TodoInput.js export function TodoInput({ value, setValue, … }) {
function handleChange(e) { setValue(e.target.value); } return ( <input value={value} onChange={handleChange} … /> ); }
// pure-components/TodoInput.js export function TodoInput({ value, setValue, … }) {
function handleChange(e) { setValue(e.target.value); } return ( <input value={value} onChange={handleChange} … /> ); }
// pure-components/TodoInput.js export function TodoInput({ value, setValue, … }) {
function handleChange(e) { setValue(e.target.value); } return ( <input value={value} onChange={handleChange} … /> ); }
// pure-components/TodoList.js export function TodoList({ todos }) { return (
<ol> {todos.map(({ id, title }) => ( <TodoListItem id={id}>{title}</TodoListItem> ))} </ol> ); }
Pure components - São componentes funcionais: dadas as mesmas props,
a renderização é a mesma (se a = b, então f(a) = f(b));
Pure components - Recebem, exclusivamente via props, valores de estado
e callbacks para atualizar esse estado; - Podem ter sua performance otimizada com React.memo.
export function Todos() { const [title, setTitle] = useState(""); const
todos = useContext(TodoStateContext); … useEffect(() => { … }, [todos, id]); function addTodo() { … } function handleTitleChange() { … } … return ( <> <input value={title} onChange={handleTitleChange} … /> … <button onClick={addTodo}>Add Todo</button> <ol> {todos.map(({ id, title }) => ( …
export function Todos() { const [title, setTitle] = useState(""); const
todos = useContext(TodoStateContext); … useEffect(() => { … }, [todos, id]); function addTodo() { … } function handleTitleChange() { … } … return ( <> <input value={title} onChange={handleTitleChange} … /> … <button onClick={addTodo}>Add Todo</button> <ol> {todos.map(({ id, title }) => ( …
// logic-hooks/useTodos export function useTodos() { const [title, setTitle] =
useState(""); const todos = useContext(TodoStateContext); … useEffect(() => { … }, [todos, id]); function addTodo() { … } return { title, setTitle, addTodo, todos, … }; }
// logic-hooks/useTodos export function useTodos() { const [title, setTitle] =
useState(""); const todos = useContext(TodoStateContext); … useEffect(() => { … }, [todos, id]); function addTodo() { … } return { title, setTitle, addTodo, todos, … }; }
// logic-hooks/useTodos export function useTodos() { const [title, setTitle] =
useState(""); const todos = useContext(TodoStateContext); … useEffect(() => { … }, [todos, id]); function addTodo() { … } return { title, setTitle, addTodo, todos, … }; }
// logic-hooks/useTodos export function useTodos() { const [title, setTitle] =
useState(""); const todos = useContext(TodoStateContext); … useEffect(() => { … }, [todos, id]); function addTodo() { … } return { title, setTitle, addTodo, todos, … }; }
Logic hooks - São custom hooks; - Cuidam da lógica
de negócio;
Logic hooks - Se comunicam com contexts, actions, stores, API,
localstorage, etc; - Retornam valores de estado e callbacks para atualizar esse estado.
// interfaces/TodoInterface.js export function TodoInterface() { const { title, setTitle,
addTodo, todos, … } = useTodos(); return ( <> <TodoInput value={title} setValue={setTitle} … /> … <Button onClick={addTodo}>Add Todo</Button> <TodoList todos={todos} /> …
// interfaces/TodoInterface.js export function TodoInterface() { const { title, setTitle,
addTodo, todos, … } = useTodos(); return ( <> <TodoInput value={title} setValue={setTitle} … /> … <Button onClick={addTodo}>Add Todo</Button> <TodoList todos={todos} /> …
// interfaces/TodoInterface.js export function TodoInterface() { const { title, setTitle,
addTodo, todos, … } = useTodos(); return ( <> <TodoInput value={title} setValue={setTitle} … /> … <Button onClick={addTodo}>Add Todo</Button> <TodoList todos={todos} /> …
// interfaces/TodoInterface.js export function TodoInterface() { const { title, setTitle,
addTodo, todos, … } = useTodos(); return ( <> <TodoInput value={title} setValue={setTitle} … /> … <Button onClick={addTodo}>Add Todo</Button> <TodoList todos={todos} /> …
Interfaces - São componentes-função (!== componentes funcionais); - Renderizam pure
components; - Recebem valores fornecidos por Logic Hooks, e os distribuem para pure components.
Logic hook → Interface → Pure component Fluxo de dados
Logic Hooks → LOOKS
E como isso resolve o problema do Todo List na
prática?
None
None
// interfaces/TodoInterface.js export function TodoInterface() { const { title, setTitle,
description, setDescription, addTodo, todos, currentTodo } = useTodos(); return ( <> <TodoInput value={title} setValue={setTitle} /> <TodoInput value={description} setValue={setDescription} /> <Button onClick={addTodo}>Add Todo</Button> <TodoList todos={todos} /> … <TodoTitle>{currentTodo.title}</TodoTitle> <TodoDescription>{currentTodo.description}</TodoDescription> …
// interfaces/TodoInterface.js export function TodoInterface() { const { title, setTitle,
description, setDescription, addTodo, todos, currentTodo } = useTodos(); return ( <> <TodoInput value={title} setValue={setTitle} /> <TodoInput value={description} setValue={setDescription} /> <Button onClick={addTodo}>Add Todo</Button> <TodoList todos={todos} /> … <TodoTitle>{currentTodo.title}</TodoTitle> <TodoDescription>{currentTodo.description}</TodoDescription> …
// interfaces/TodoInterface.js export function TodoInterface() { const { title, setTitle,
description, setDescription, addTodo, todos } = useTodos(); return ( <> <TodoInput value={title} setValue={setTitle} /> <TodoInput value={description} setValue={setDescription} /> <Button onClick={addTodo}>Add Todo</Button> <TodoList todos={todos} /> …
// interfaces/TodoContentInterface.js export function TodoContentInterface() { const { currentTodo }
= useTodos(); return ( <> <TodoTitle>{currentTodo.title}</TodoTitle> <TodoDescription> {currentTodo.description} </TodoDescription> </> ); }
Vantagens - Permite reutilizar a mesma lógica de estado com
diferentes interfaces e vice-versa ; - Componentes que são fracamente acoplados podem ser testados e otimizados com mais facilidade;
Vantagens - Torna mais fácil refatorar e deletar código; -
Logic hooks podem ser consumidos sem conhecimento sobre seu funcionamento interno; - Monorepo: um logic hook pode ser um package.
Desvantagens - Não é ideal para aplicações mais simples, onde
não há necessidade frequente de reutilizar código; - Muitas camadas (lasagna code) se utilizada com Flux; Action → Dispatcher → Store → Logic hook → Interface → Pure component - Pouca documentação.
https:/ /codesandbox.io/s/talk-logic-hooks-ct0jb
Dúvidas?
Para quem quiser entrar em contato: Twitter: @charbelrami GitHub: /charbelrami
Regras para participação no sorteio do DRONE: 1. Seguir o
perfil @VagasTech no Twitter 2. Dar RT no Tweet fixado em nosso perfil 3. Estar presente na hora do sorteio :)
None