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
Arquitetura para a nova geração de aplicações
Search
Charbel
October 19, 2019
Programming
0
130
Arquitetura para a nova geração de aplicações
Talk @ React Conf Brasil. Logic Hooks, Interfaces, Pure Components
Charbel
October 19, 2019
Tweet
Share
Other Decks in Programming
See All in Programming
Automatic Grammar Agreementと Markdown Extended Attributes について
kishikawakatsumi
0
200
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
7
4k
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.6k
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.4k
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
660
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
210
Oxlintはいいぞ
yug1224
5
1.4k
[KNOTS 2026登壇資料]AIで拡張‧交差する プロダクト開発のプロセス および携わるメンバーの役割
hisatake
0
300
CSC307 Lecture 05
javiergs
PRO
0
500
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
390
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
230
Apache Iceberg V3 and migration to V3
tomtanaka
0
170
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
130
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.7k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
170
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
120
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
340
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
The Pragmatic Product Professional
lauravandoore
37
7.1k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
54
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
130
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