Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Smart Nano Stores, or how we made front-end simpler (for the FOF meetup in Lisbon)

Smart Nano Stores, or how we made front-end simpler (for the FOF meetup in Lisbon)

Nina Torgunakova

November 09, 2023
Tweet

More Decks by Nina Torgunakova

Other Decks in Programming

Transcript

  1. The modern Web Development World is becoming more and more

    complex. Who suffers from it? 5 @evilmartians
  2. The average size of web pages is constantly growing up

    9 * Source: www.keycdn.com/support/the-growth-of-web-page-size @evilmartians
  3. 11 Often, to do the same amount of work, more

    programmers are needed. Now Before @evilmartians
  4. 12 Result: the growing complexity of Web Development makes everyone

    frustrated Users Developers Business @evilmartians
  5. Fold knowledge into data, so program logic can be stupid

    and robust. 14 Eric Raymond, “Rule of Representation” @evilmartians
  6. 17

  7. 21 Use stores to move logic outside of components @evilmartians

    Component Component Component Store Store
  8. Bad programmers worry about the code. Good programmers worry about

    data structures and their relationships. 23 Linus Torvalds @evilmartians
  9. 24 Global State Any State Change All Components Call the

    selector function Popular global state approach: @evilmartians
  10. 27 Applying to State Management: Component Component Component Subscriber 1

    Subscriber 2 Subscriber 3 @evilmartians Small Store Small Store Small Store
  11. 1. Move logic to stores outside of component 2. Make

    stores small and smart Let's go further! 30 @evilmartians
  12. 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
  13. 1. Move logic to stores outside of components. 35 Component

    Component Component Store Store @evilmartians
  14. 3. Provide minimalism and simplicity. 37 Store One-line operations: •

    Create store • Get value • Set value @evilmartians Get value Set value
  15. 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
  16. 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
  17. Creating atom store for search input: // core/state.ts import {

    atom } from 'nanostores'; export const $searchInput = atom<string>(''); 46 @evilmartians
  18. 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
  19. 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
  20. 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
  21. 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