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

Unveiling Nano Stores

Unveiling Nano Stores

Nina Torgunakova

September 23, 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. We need to do a lot of actions to make

    something simple. 7 @evilmartians
  3. The average size of web pages is constantly growing up

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

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

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

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

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

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

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

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

    Subscriber 2 Subscriber 3 @evilmartians Small Store Small Store Small Store
  12. Perfection is achieved, not when there is nothing more to

    add, but when there is nothing left to take away. 30 Antoine de Saint-Exupéry @evilmartians
  13. Everything should be made as simple as possible, but no

    simpler. 33 Albert Einstein @evilmartians
  14. We need to consider many different challenges: - Synchronizing changes

    between browser tabs - Implementing SPA navigation - Automatic translation of content - CRDT conflict resolution …and more… 35 @evilmartians
  15. 1. Move logic to stores outside of components. 37 Component

    Component Component Store Store @evilmartians
  16. 2. Provide minimalism and simplicity. 38 Store One-line operations: •

    Create store • Get value • Set value @evilmartians Get value Set value
  17. 4. Consider developer challenges. 40 SPA Navigation Smart data fetching

    Automatic Translations Conflict resolution Synchronizing @evilmartians
  18. 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 } ]); 45 @evilmartians
  19. 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} />)}</>; }; 46 @evilmartians
  20. Creating atom store for search input: // core/state.ts import {

    atom } from 'nanostores'; export const $searchInput = atom<string>(''); 49 @evilmartians
  21. 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} />; }; 50 @evilmartians
  22. 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)); } ); 51 @evilmartians
  23. 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} />)}</>; }; 52 @evilmartians
  24. So, what is next? 1. Try out a new approach

    to state management in your own project. 2. See the power of simplicity! 54 @evilmartians github.com/nanostores