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
Smart Nano Stores, or how we made front-end sim...
Search
Nina Torgunakova
November 09, 2023
Programming
1
330
Smart Nano Stores, or how we made front-end simpler (for the FOF meetup in Lisbon)
Nina Torgunakova
November 09, 2023
Tweet
Share
More Decks by Nina Torgunakova
See All by Nina Torgunakova
Smart nano stores for state management, or how we made front-end simpler
ninoid
0
500
Unveiling Nano Stores
ninoid
0
570
Умные nano stores, или как мы сделали веб-разработку проще
ninoid
0
820
Solving algorithms: beyond cramming for job interviews
ninoid
0
1.6k
Other Decks in Programming
See All in Programming
メルカリ ハロ アプリの技術スタック
atsumo
2
750
ファーストペンギンBot @Qiita Hackathon 2024 予選
dyson_web
0
220
自分だけの世界を創るクリエイティブコーディング / Creative Coding: Creating Your Own World
chobishiba
2
840
ACES Meet におけるリリース作業改善の取り組み
fukucheee
0
130
[PHPカンファレンス沖縄2024]「無理なくできるだけ安全に」テストもないレガシーコードをリファクタリングするテクニック
ikezoemakoto
3
120
VS Code extension: ドラッグ&ドロップでファイルを並び替える
ttrace
0
170
선언형 UI를 학습할 때 알아둬야하는 키워드들
l2hyunwoo
0
140
DevFest Android in Korea 2024 - 안드로이드의 문단속 : 앱을 지키는 암호화 이야기
mdb1217
1
150
NEWTにおけるiOS18対応の進め方
ryu1sazae
0
230
GitHub Copilot Workspace で我々のアプリ開発がどう変わるのか?
shuyakinjo
0
900
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
1.2k
.NET Aspireのクラウド対応検証: Azureと他環境での実践
ymd65536
1
410
Featured
See All Featured
How GitHub Uses GitHub to Build GitHub
holman
473
290k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.3k
Web Components: a chance to create the future
zenorocha
310
42k
How STYLIGHT went responsive
nonsquared
95
5.1k
Being A Developer After 40
akosma
84
590k
What the flash - Photography Introduction
edds
67
11k
Pencils Down: Stop Designing & Start Developing
hursman
119
11k
Designing for Performance
lara
604
68k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
38
2.1k
Mobile First: as difficult as doing things right
swwweet
222
8.8k
Art, The Web, and Tiny UX
lynnandtonic
296
20k
From Idea to $5000 a Month in 5 Months
shpigford
380
46k
Transcript
Smart nano stores for state management 1 @evilmartians or how
we made front-end simpler
Nina Torgunakova Frontend Engineer 2 @evilmartians
3 @evilmartians What we had before
4 What we have today * Source: www.medium.com/@withinsight1/the-front-end-spectrum-c0f30998c9f0/ @evilmartians
The modern Web Development World is becoming more and more
complex. Who suffers from it? 5 @evilmartians
1. Developers 6 @evilmartians
7 Everything is built around frameworks but remains complex @evilmartians
2. Users 8 @evilmartians
The average size of web pages is constantly growing up
9 * Source: www.keycdn.com/support/the-growth-of-web-page-size @evilmartians
3. Business 10 @evilmartians
11 Often, to do the same amount of work, more
programmers are needed. Now Before @evilmartians
12 Result: the growing complexity of Web Development makes everyone
frustrated Users Developers Business @evilmartians
13 Let's try to find inspiration! @evilmartians
Fold knowledge into data, so program logic can be stupid
and robust. 14 Eric Raymond, “Rule of Representation” @evilmartians
15 Framework Logic UI Features Data @evilmartians
But what if we made a mistake? 16 @evilmartians
17
None
19 Data Logic UI Features @evilmartians Framework Logic UI Features
Data Framework
State Management 20 Application Data State @evilmartians
21 Use stores to move logic outside of components @evilmartians
Component Component Component Store Store
OK, any other ideas about the ideal process of state
management? 22 @evilmartians
Bad programmers worry about the code. Good programmers worry about
data structures and their relationships. 23 Linus Torvalds @evilmartians
24 Global State Any State Change All Components Call the
selector function Popular global state approach: @evilmartians
25 How about improving data structure and making it faster?
@evilmartians
26 Reactive programming approach: @evilmartians
27 Applying to State Management: Component Component Component Subscriber 1
Subscriber 2 Subscriber 3 @evilmartians Small Store Small Store Small Store
28 But what if some data depends on other data?
@evilmartians
29 Solution: Relationships between stores Store Store Store Store @evilmartians
Component Component
1. Move logic to stores outside of component 2. Make
stores small and smart Let's go further! 30 @evilmartians
Perfection is achieved, not when there is nothing more to
add, but when there is nothing left to take away. 31 Antoine de Saint-Exupéry @evilmartians
32 @evilmartians State Management in modern frameworks
33 Smart Store Set value Get value @evilmartians
34 Let's summarize! @evilmartians
1. Move logic to stores outside of components. 35 Component
Component Component Store Store @evilmartians
2. Make stores smart. 36 Store Store Store Store @evilmartians
Store Component Component
3. Provide minimalism and simplicity. 37 Store One-line operations: •
Create store • Get value • Set value @evilmartians Get value Set value
38 ??? What state manager will solve all the problems?
@evilmartians
39 Nano Stores @evilmartians
Let's build a simple online catalog: 40 @evilmartians
41 @evilmartians
Creating atom store for products in catalog: // core/state.ts import
{ atom } from 'nanostores'; export const $products = atom<Product[]>([ { name: 'Nano Phone', price: 30 }, { name: 'Nano TV', price: 75 }, { name: 'Nano Laptop', price: 75 } ]); 42 @evilmartians
Using atom store: // components/Catalog.tsx import { useStore } from
'@nanostores/react'; import { $products } from 'core/state'; const Catalog = () => { const products = useStore($products); return <>{products.map((product) => <Card product={product} />)}</>; }; 43 @evilmartians
44 @evilmartians
Adding filtering using search input 45 @evilmartians
Creating atom store for search input: // core/state.ts import {
atom } from 'nanostores'; export const $searchInput = atom<string>(''); 46 @evilmartians
Using atom store for search input: // components/SearchBar.tsx import {
useStore } from '@nanostores/react'; import { $searchInput } from 'core/state'; const SearchBar = () => { const searchInput = useStore($searchInput); return <input onChange={(e) => $searchInput.set(e.target.value)} value={searchInput} />; }; 47 @evilmartians
Adding computed store to filter catalog: // core/state.ts import {
computed } from 'nanostores'; export const $filteredProducts = computed( [$searchInput, $products], (searchInput, products) => { return products.filter(product => product.name.includes(searchInput)); } ); 48 @evilmartians
Using computed store in components: // components/Catalog.tsx import { useStore
} from '@nanostores/react'; import { $filteredProducts } from 'core/state'; // Usage is exactly the same: const Catalog = () => { const filteredProducts = useStore($filteredProducts); return <>{filteredProducts.map((product) => <Card product={product} />)}</>; }; 49 @evilmartians
50 @evilmartians
So, what is next? 1. Try out a new approach
to state management in your own project. 2. See the power of simplicity! 51 @evilmartians github.com/nanostores
ninaTorgunakova 52 @evilmartians Questions? github.com/nanostores