<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xmlns:tt="http://teletype.in/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>Doniyor</title><generator>teletype.in</generator><description><![CDATA[27 y.o Senior Frontend developer at Uzum Technologies with 6+ years of experience. Posting some good frontend stuff on t.me/peaceofcode.]]></description><image><url>https://img1.teletype.in/files/c1/c3/c1c3e9bb-df39-4124-9917-b6daef999994.png</url><title>Doniyor</title><link>https://teletype.in/@dyusupov</link></image><link>https://teletype.in/@dyusupov?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=dyusupov</link><atom:link rel="self" type="application/rss+xml" href="https://teletype.in/rss/dyusupov?offset=0"></atom:link><atom:link rel="next" type="application/rss+xml" href="https://teletype.in/rss/dyusupov?offset=10"></atom:link><atom:link rel="search" type="application/opensearchdescription+xml" title="Teletype" href="https://teletype.in/opensearch.xml"></atom:link><pubDate>Thu, 21 May 2026 06:42:22 GMT</pubDate><lastBuildDate>Thu, 21 May 2026 06:42:22 GMT</lastBuildDate><item><guid isPermaLink="true">https://teletype.in/@dyusupov/vue3-watchers</guid><link>https://teletype.in/@dyusupov/vue3-watchers?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=dyusupov</link><comments>https://teletype.in/@dyusupov/vue3-watchers?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=dyusupov#comments</comments><dc:creator>dyusupov</dc:creator><title>Vue 3 da watch methodi</title><pubDate>Sun, 24 Nov 2024 19:34:13 GMT</pubDate><media:content medium="image" url="https://img1.teletype.in/files/09/60/0960b6fc-99eb-4213-a9b1-47eb5204b0b4.png"></media:content><category>Vue</category><description><![CDATA[<img src="https://img2.teletype.in/files/db/51/db51183b-c75b-4332-8f2e-86ef7748e5d6.png"></img>Vue 3 Watchers]]></description><content:encoded><![CDATA[
  <figure id="Cxcc" class="m_column">
    <img src="https://img2.teletype.in/files/9c/8f/9c8fc37e-2f73-49bd-b1c5-97a63f8ca197.png" width="1418" />
  </figure>
  <p id="q4sj">watch() metodi reaktiv ma&#x27;lumotlardagi o&#x27;zgarishlarni kuzatish va ularga javob qaytarish uchun ishlatiladi. Bu metod quyidagi asosiy xususiyatlarga ega:</p>
  <h2 id="QAjo">Asosiy xususiyatlari</h2>
  <ol id="H4ZU">
    <li id="Ef7Z">Side Effects: Reactive data o&#x27;zgarganda side effect&#x27;larni bajarish imkonini beradi</li>
    <li id="fxlF">Async Operations: Async operatsiyalarni bajarish imkoniyati</li>
    <li id="Yc9M">Control: Watching boshlanishi va to&#x27;xtatilishini nazorat qilish</li>
    <li id="B1Dy">Deep watching: Obyektlarni chuqur kuzatish imkoniyati</li>
  </ol>
  <h2 id="ZbXg">Basic Usage</h2>
  <pre id="pSiL" data-lang="typescript">import { ref, watch } from &#x27;vue&#x27;

const searchQuery = ref(&#x27;&#x27;)
const searchResults = ref([])

// Basic watcher
watch(searchQuery, async (newQuery, oldQuery) =&gt; {
  if (newQuery.trim()) {
    const results = await fetchSearchResults(newQuery)
    searchResults.value = results
  } else {
    searchResults.value = []
  }
})</pre>
  <h2 id="OxyR">Multiple Sources</h2>
  <p id="UUMd">Bir nechta source&#x27;larni kuzatish mumkin:</p>
  <pre id="G8gQ" data-lang="typescript">const firstName = ref(&#x27;John&#x27;)
const lastName = ref(&#x27;Doe&#x27;)

watch([firstName, lastName], ([newFirst, newLast], [oldFirst, oldLast]) =&gt; {
  console.log(&#x60;Name changed from ${oldFirst} ${oldLast} to ${newFirst} ${newLast}&#x60;)
})</pre>
  <h2 id="wJdd">Deep Watching</h2>
  <p id="DD4l">Obyekt ichidagi o&#x27;zgarishlarni ham kuzatish:</p>
  <pre id="hcbN" data-lang="typescript">const user = reactive({
  name: &#x27;John&#x27;,
  profile: {
    age: 25,
    city: &#x27;Tashkent&#x27;
  }
})

// Deep watcher
watch(user, (newValue, oldValue) =&gt; {
  console.log(&#x27;User data changed:&#x27;, newValue)
}, { deep: true })</pre>
  <h2 id="0EfF">Immediate Watching</h2>
  <p id="LTqx">Watcher ni darhol ishga tushirish:</p>
  <pre id="AY4W" data-lang="typescript">const todoId = ref(1)
const todoData = ref(null)

watch(todoId, async (newId) =&gt; {
  todoData.value = await fetchTodo(newId)
}, { immediate: true })</pre>
  <h2 id="J4mK">Watch vs watchEffect</h2>
  <p id="v0AM">Vue 3 da ikkita watching mexanizmi mavjud:</p>
  <h3 id="YJ7X">watch</h3>
  <ul id="i3sM">
    <li id="JD1E">Aniq source va callback bilan ishlaydi</li>
    <li id="gG34">Old va new qiymatlarni taqdim etadi</li>
    <li id="Yx4h">Watching qachon ishga tushishini nazorat qilish mumkin</li>
  </ul>
  <pre id="5djS" data-lang="typescript">const counter = ref(0)
watch(counter, (newValue, oldValue) =&gt; {
  console.log(&#x60;Counter changed from ${oldValue} to ${newValue}&#x60;)
})</pre>
  <h3 id="V9or">watchEffect</h3>
  <ul id="CJfx">
    <li id="GFUJ">Avtomatik ravishda dependency&#x27;larni kuzatadi</li>
    <li id="zGhq">Callback ichida ishlatilgan har qanday reactive value o&#x27;zgarganda ishga tushadi</li>
    <li id="JSyC">Darhol ishga tushadi</li>
  </ul>
  <pre id="9txc" data-lang="typescript">const todos = ref([])
const error = ref(null)

watchEffect(async () =&gt; {
  try {
    todos.value = await fetchTodos()
  } catch (e) {
    error.value = e
  }
})</pre>
  <h2 id="1t1P">Stop Watching</h2>
  <p id="h1ww">Watcher&#x27;ni to&#x27;xtatish:</p>
  <pre id="ZRCg" data-lang="typescript">const stop = watch(source, callback)
// Kerak bo&#x27;lganda
stop()</pre>
  <h2 id="T91Y">Best Practices</h2>
  <ol id="qnT6">
    <li id="LMoK"><strong>Cleanup function ishlatish</strong></li>
  </ol>
  <pre id="tpF8" data-lang="typescript">watch(id, async (newId, oldId, onCleanup) =&gt; {
  const { response, cancel } = doAsyncWork(newId)
  
  // Agar yangi watcher ishga tushsa, oldingi requestni bekor qilish
  onCleanup(() =&gt; cancel())
  
  data.value = await response
})</pre>
  <ol id="am72">
    <li id="QSFh"><strong>Error Handling</strong></li>
  </ol>
  <pre id="yu2E" data-lang="typescript">watch(source, async (value) =&gt; {
  try {
    await someAsyncOperation()
  } catch (error) {
    console.error(&#x27;Error in watcher:&#x27;, error)
  }
})</pre>
  <ol id="ATF0">
    <li id="EUpA"><strong>Debouncing</strong></li>
  </ol>
  <pre id="OQO9" data-lang="typescript">import { debounce } from &#x27;lodash&#x27;

const debouncedWatch = watch(searchInput, debounce((value) =&gt; {
  // Search logic here
}, 300))</pre>
  <h2 id="xE1C"><br />Anti-patterns va Ogohlantirishlar</h2>
  <h3 id="JrSl">1. Computed property o&#x27;rniga watch ishlatish</h3>
  <pre id="v1gz" data-lang="typescript">// ❌ Yomon
watch([firstName, lastName], ([first, last]) =&gt; {
  fullName.value = &#x60;${first} ${last}&#x60;
})

// ✅ Yaxshi
const fullName = computed(() =&gt; &#x60;${firstName.value} ${lastName.value}&#x60;)</pre>
  <p id="7eZd"><strong>Nega bu anti-pattern?</strong></p>
  <ul id="YLeX">
    <li id="S02b"><strong>Performance</strong>: Watch har doim callback funksiyani to&#x27;liq bajaradi, computed esa keshlanadi va faqat dependency o&#x27;zgargandagina qayta hisoblanadi</li>
    <li id="aNll"><strong>Code readability</strong>: Computed aniq ko&#x27;rsatadiki bu yangi value hisoblanmoqda. Watch esa side-effect uchun mo&#x27;ljallangan</li>
    <li id="FBxb"><strong>Debugging</strong>: Computed bilan value qayerdan kelayotganini kuzatish osonroq</li>
    <li id="GM30"><strong>Testing</strong>: Computed propertylarni test qilish osonroq chunki ular pure function</li>
  </ul>
  <h3 id="oaEk">2. Bir watcher ichida ko&#x27;p vazifalarni bajarish</h3>
  <pre id="2xuD" data-lang="stylus">// ❌ Yomon
watch(user, () =&gt; {
  updateProfile()
  sendAnalytics()
  refreshUI()
  loadRelatedData()
})

// ✅ Yaxshi - Alohida watcherlar
watch(user, updateProfile)
watch(user, sendAnalytics)
watch(user, refreshUI)
watch(user, loadRelatedData)</pre>
  <p id="aYOd"><strong>Nega bu anti-pattern?</strong></p>
  <ul id="xfmX">
    <li id="3PPZ"><strong>Maintainability</strong>: Har bir watcher alohida mas&#x27;uliyatga ega bo&#x27;lishi kerak (Single Responsibility Principle)</li>
    <li id="QeHW"><strong>Error handling</strong>: Bitta funksiyada xato chiqsa, boshqa funksiyalar ham ishlamay qoladi</li>
    <li id="C4dA"><strong>Testing</strong>: Ko&#x27;p vazifali watcherni test qilish murakkab</li>
    <li id="BLzi"><strong>Debug qilish</strong>: Xato qaysi operatsiyada yuz berganini aniqlash qiyin</li>
  </ul>
  <h3 id="NgpE">3. Watch ichida reactive data o&#x27;zgartirish</h3>
  <pre id="wqeQ" data-lang="typescript">// ❌ Yomon
watch(count, (newCount) =&gt; {
  anotherCount.value = newCount * 2
})

// ✅ Yaxshi
const anotherCount = computed(() =&gt; count.value * 2)</pre>
  <p id="7UYW"><strong>Nega bu anti-pattern?</strong></p>
  <ul id="28yl">
    <li id="EOd7"><strong>Infinite loop xavfi</strong>: Agar watched value watcherni ichida o&#x27;zgartirilsa, infinite loop yuzaga kelishi mumkin</li>
    <li id="esoa"><strong>State management murakkabligi</strong>: Reactive datani bir necha joyda o&#x27;zgartirish debug qilishni qiyinlashtiradi</li>
    <li id="6vuT"><strong>Vue&#x27;ning reaktivlik tizimidan noto&#x27;g&#x27;ri foydalanish</strong>: Bu vazifa computed property uchun mo&#x27;ljallangan</li>
  </ul>
  <h3 id="VSV9">4. Watcherda og&#x27;ir operatsiyalarni to&#x27;g&#x27;ridan-to&#x27;g&#x27;ri bajarish</h3>
  <pre id="wX1p" data-lang="typescript">// ❌ Yomon
watch(searchQuery, (query) =&gt; {
  const results = heavyComputation(query)
  searchResults.value = results
})

// ✅ Yaxshi
watch(searchQuery, debounce(async (query) =&gt; {
  const results = await heavyComputation(query)
  searchResults.value = results
}, 300))</pre>
  <p id="xjkY"><strong>Nega bu anti-pattern?</strong></p>
  <ul id="1xHV">
    <li id="ZN6D"><strong>Performance</strong>: Har bir o&#x27;zgarishda og&#x27;ir hisob-kitoblar UI ni sekinlashtiradi</li>
    <li id="fmCZ"><strong>User Experience</strong>: Foydalanuvchi kiritayotgan vaqtda ham hisob-kitoblar amalga oshiriladi</li>
    <li id="S1pq"><strong>Resource usage</strong>: Keraksiz hisob-kitoblar tizim resurslarini behuda sarflaydi</li>
  </ul>
  <h3 id="xprL">5. Deep watching ni keraksiz ishlatish</h3>
  <pre id="AibQ" data-lang="typescript">// ❌ Yomon
watch(complexObject, (newVal) =&gt; {
  // Something with newVal
}, { deep: true })

// ✅ Yaxshi - Faqat kerakli propertyni kuzatish
watch(() =&gt; complexObject.specificProperty, (newVal) =&gt; {
  // Something with newVal
})</pre>
  <p id="Aej8"><strong>Nega bu anti-pattern?</strong></p>
  <ul id="rzaX">
    <li id="4RA2"><strong>Performance</strong>: Deep watching katta obyektlar uchun sezilarli performance yo&#x27;qotishga olib keladi</li>
    <li id="k6KQ"><strong>Unnecessary updates</strong>: Keraksiz propertylar o&#x27;zgarganda ham watcher ishga tushadi</li>
    <li id="ezy3"><strong>Memory usage</strong>: Vue har bir property uchun reactive wrapper yaratishi kerak bo&#x27;ladi</li>
  </ul>
  <h3 id="pibR">6. watchEffect ichida async operatsiyalarni noto&#x27;g&#x27;ri boshqarish</h3>
  <pre id="nxK3" data-lang="typescript">// ❌ Yomon
watchEffect(async () =&gt; {
  const data = await fetchData()
  results.value = data
})

// ✅ Yaxshi
watchEffect((onCleanup) =&gt; {
  const controller = new AbortController()
  onCleanup(() =&gt; controller.abort())
  
  fetchData({ signal: controller.signal })
    .then(data =&gt; {
      results.value = data
    })
})</pre>
  <p id="1MOp"><strong>Nega bu anti-pattern?</strong></p>
  <ul id="wxl3">
    <li id="jAy7"><strong>Race conditions</strong>: Oldingi request tugatilmasdan yangi request boshlanishi mumkin</li>
    <li id="w4o9"><strong>Memory leaks</strong>: Cleanup funksiyasisiz eskii requestlar to&#x27;planib qolishi mumkin</li>
    <li id="QmJI"><strong>State inconsistency</strong>: Natijalar noto&#x27;g&#x27;ri tartibda kelishi mumkin<br /></li>
  </ul>
  <h2 id="ArMi">Best Practice Recommendations</h2>
  <ol id="ocka">
    <li id="xowC"><strong>Watch o&#x27;rniga computed ishlatish mumkin bo&#x27;lgan holatlarni aniqlang</strong></li>
    <ul id="yYtb">
      <li id="qH96">Agar yangi value hisoblash kerak bo&#x27;lsa - computed ishlating</li>
      <li id="DcJb">Agar side-effect (API call, DOM manipulation) kerak bo&#x27;lsa - watch ishlating</li>
    </ul>
    <li id="cTV2"><strong>Watcherlarni kichik va aniq vazifalar uchun ajrating</strong></li>
    <ul id="ZMOa">
      <li id="QPAl">Har bir watcher bitta vazifani bajarsin</li>
      <li id="uopP">Vazifalar bir-biriga bog&#x27;liq bo&#x27;lsa, alohida composable funksiya yarating</li>
    </ul>
    <li id="RkdX"><strong>Async operatsiyalar uchun cleanup ishlatishni unutmang</strong></li>
    <ul id="D0T3">
      <li id="KT13">Race conditionlardan qochish uchun</li>
      <li id="Ab4V">Memory leaklarni oldini olish uchun</li>
      <li id="7qVN">Network requestlarni to&#x27;g&#x27;ri boshqarish uchun</li>
    </ul>
    <li id="cH0X"><strong>Watch va watchEffect ni to&#x27;g&#x27;ri tanlang</strong></li>
    <ul id="vZk8">
      <li id="AUol">Watch - aniq dependency va old/new value kerak bo&#x27;lganda</li>
      <li id="XB3j">watchEffect - dependency avtomatik aniqlanishi kerak bo&#x27;lganda</li>
    </ul>
    <li id="5g4q"><strong>Performance optimizatsiyasi</strong></li>
    <ul id="9W26">
      <li id="fPdt">Deep watching o&#x27;rniga specific propertylarni kuzating</li>
      <li id="3Gjj">Debounce/throttle qo&#x27;llang</li>
      <li id="BRru">Og&#x27;ir operatsiyalarni optimize qiling</li>
    </ul>
  </ol>
  <p id="gKB8"></p>
  <p id="kfny">Hozircha shu, keyingi postlarda ko&#x27;rishguncha. Stay tuned...<br /><a href="https://t.me/peaceofcode" target="_blank">@peaceofcode</a></p>

]]></content:encoded></item><item><guid isPermaLink="true">https://teletype.in/@dyusupov/vue-computed</guid><link>https://teletype.in/@dyusupov/vue-computed?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=dyusupov</link><comments>https://teletype.in/@dyusupov/vue-computed?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=dyusupov#comments</comments><dc:creator>dyusupov</dc:creator><title>Computed properties</title><pubDate>Tue, 27 Aug 2024 11:15:12 GMT</pubDate><media:content medium="image" url="https://img1.teletype.in/files/8c/07/8c076fd0-9812-4d3c-8b67-43d15d48ec1e.png"></media:content><category>Vue</category><description><![CDATA[<img src="https://img4.teletype.in/files/30/1b/301b9591-b27e-419a-b9e7-2704d7c2b78a.jpeg"></img>computed() method reaktiv va kuzatuvchan xususiyatlarni yaratish uchun ishlatiladi. Bu metod quyidagi asosiy xususiyatlarga ega:]]></description><content:encoded><![CDATA[
  <figure id="6uWi" class="m_column">
    <img src="https://img4.teletype.in/files/30/1b/301b9591-b27e-419a-b9e7-2704d7c2b78a.jpeg" width="1260" />
  </figure>
  <p id="KL7e"><code>computed()</code> method reaktiv va kuzatuvchan xususiyatlarni yaratish uchun ishlatiladi. Bu metod quyidagi asosiy xususiyatlarga ega:</p>
  <ol id="LpjO">
    <li id="5z5c">Create aggregate value: Boshqa reaktiv ma&#x27;lumotlarga asoslangan qiymatlarni hisoblaydi.</li>
    <li id="RE7w">Caching: Natijani keshlashtiradi va faqat bog&#x27;liq ma&#x27;lumotlar o&#x27;zgargandagina qayta hisoblaydi.</li>
    <li id="ID49">Getter function: <code>computed()</code> ga uzatiladigan funksiya getter sifatida ishlaydi.</li>
    <li id="fAgN">Reactivity: Hisoblangan xususiyat o&#x27;zgarsa, undan foydalanuvchi komponentlar avtomatik ravishda yangilanadi.</li>
  </ol>
  <h3 id="fUh1">Create aggregated value</h3>
  <pre id="H1Sv" data-lang="javascript">import { computed, ref } from &#x27;vue&#x27;

const user = reactive({
  firstName: &#x27;Doniyor&#x27;,
  lastName: &#x27;Yusupov&#x27;,
  status: 1
})

const statusMap = {
 0: &#x27;Created&#x27;,
 1: &#x27;Verified&#x27;
}

const fullName = computed(() =&gt; &#x60;${user.firstName} ${user.lastName}&#x60;) // Doniyor Yusupov

const userStatus = computed(() =&gt; statusMap[user.status]) // &#x27;Verified&#x27;</pre>
  <p id="ppGl">Bu Create aggregate value uchun misol edi. Ya&#x27;ni bir reactive object dan bir nechta reactive object larni create qilishimiz mumkin.</p>
  <p id="4hie"></p>
  <h3 id="BTFi">Caching</h3>
  <pre id="XZDb" data-lang="javascript">import { ref, computed } from &#x27;vue&#x27;

const numbers = ref(Array.from({ length: 10000 }, (_, i) =&gt; i + 1))
const searchTerm = ref(&#x27;&#x27;)

const filteredAndSortedNumbers = computed(() =&gt; {
  return numbers.value
    .filter(num =&gt; num.toString().includes(searchTerm.value))
    .sort((a, b) =&gt; b - a)
    .slice(0, 100)
})

function useNumbers() {
  function updateSearchTerm(newTerm) {
    searchTerm.value = newTerm
  }

  return {
    filteredAndSortedNumbers,
    updateSearchTerm
  }
}
</pre>
  <pre id="0jWA" data-lang="html">&lt;template&gt;
  &lt;div&gt;
    &lt;input v-model=&quot;searchTerm&quot; @input=&quot;updateSearchTerm&quot; placeholder=&quot;Qidirish&quot;&gt;
    &lt;ul&gt;
      &lt;li v-for=&quot;num in filteredAndSortedNumbers&quot; :key=&quot;num&quot;&gt;{{ num }}&lt;/li&gt;
    &lt;/ul&gt;
    ...
    &lt;ul&gt;
      &lt;li v-for=&quot;num in filteredAndSortedNumbers&quot; :key=&quot;num&quot;&gt;{{ num }}&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;
const { filteredAndSortedNumbers, updateSearchTerm } = useNumbers()
&lt;/script&gt;</pre>
  <p id="lha4">Bu misolda:</p>
  <ol id="6kSd">
    <li id="xUMN"><code>numbers</code> - 10,000 ta elementdan iborat katta massiv.</li>
    <li id="hnb6"><code>searchTerm</code> - foydalanuvchi kiritadigan qidiruv so&#x27;zi.</li>
    <li id="W9Jc"><code>filteredAndSortedNumbers</code> - hisoblangan xususiyat bo&#x27;lib, u quyidagi amallarni bajaradi:</li>
    <ul id="unIO">
      <li id="U8nC">Massivni filtrlaydi</li>
      <li id="USAb">Teskari tartibda saralaydi</li>
      <li id="vGGG">Natijadan faqat birinchi 100 ta elementni oladi</li>
    </ul>
  </ol>
  <p id="XvGj">Keshlashtirish afzalligi:</p>
  <ol id="JkYY">
    <li id="lqvH">Samaradorlik: <code>filteredAndSortedNumbers</code> faqat <code>numbers</code> yoki <code>searchTerm</code> o&#x27;zgargandagina qayta hisoblanadi. Aks holda, keshlashtirish natijasidan foydalaniladi.</li>
    <li id="8n2X">Takroriy hisob-kitoblardan qochish: Agar komponentda <code>filteredAndSortedNumbers</code>ga bir necha marta murojaat qilinsa ham, hisob-kitob faqat bir marta bajariladi.</li>
    <li id="oZvf">Tezkorlik: Foydalanuvchi interfeysi sezilarli darajada tezroq ishlaydi, chunki murakkab hisob-kitoblar faqat zarur bo&#x27;lgandagina bajariladi.</li>
  </ol>
  <h3 id="o55e"></h3>
  <h3 id="p4Bk">Reacts only to reactive data change</h3>
  <p id="fyt7"><code>computed() </code>methodi faqat reactive data o&#x27;zgarsagina callback ishlaydi.</p>
  <pre id="XY1D" data-lang="javascript">const now = computed(() =&gt; Date.now())
const counter = computed(() =&gt; localStorage.getItem(&#x27;counter&#x27;))</pre>
  <p id="PCQt">Bu misollarda <code>now</code> va <code>counter</code> o&#x27;zgaruvchilari bir marta initialization paytida set qilinadi va boshqa o&#x27;zgarmaydi chunki <code>Date</code> ham <code>localStorage</code> ham reactive emas.<br /></p>
  <h3 id="2viT">Writable computed - <em>Not recommended</em></h3>
  <p id="Wtxj">Ccomputed properties default holatda <strong>getter</strong> funksiyaga ega. Kamdan-kam hollarda sizga writable computed value kerak bo&#x27;ladi. Agar siz computed value ga yangi qiymat bermoqchi bo&#x27;lsangiz, Runtime warning chiqadi. Agar kerak bo&#x27;sa siz uni ham <strong>getter</strong>, ham <strong>setter</strong> funksiyalarni ta&#x27;minlash orqali yaratishingiz mumkin.</p>
  <pre id="4p9c" data-lang="javascript">import { ref, computed } from &#x27;vue&#x27;
const firstName = ref(&#x27;John&#x27;)
const lastName = ref(&#x27;Doe&#x27;)
const fullName = computed({
  // getter
  get() {
    return firstName.value + &#x27; &#x27; + lastName.value
  },
  // setter
  set(newValue) {
    [firstName.value, lastName.value] = newValue.split(&#x27; &#x27;)
  }
})</pre>
  <h3 id="VUpg"><br />Best practices</h3>
  <p id="BKTG">Computed getter funcsiyalar faqatgina Pure computation uchun va Side-effect lardan holi bo&#x27;lgan xolda ishlatilishi kerak. Masalan quyidagilarni computed bn ishlatmaslik lozim:<br /> - DOM ga o&#x27;zgartirish kiritish<br /> - boshqa state ni o&#x27;zgartirish<br /> - async request lar ishlatish</p>
  <p id="ZkEI">Computed value ni mutate qilmang. Uni faqat source state o&#x27;zgargan holda o&#x27;zgaradigan va shu value orqali hisoblangan qiymat deb qabul qiling.<br /><br />&quot;<strong>Writable computed</strong> larchi?&quot; deyishingiz mumkin. <br />Writable computedlarni tepadagi Anti-patternlar sabab ishlatish tavsiya etilmaydi. Source state o&#x27;zgarganda boshqa stateni o&#x27;zgartirish uchun yoki async request lar uchun esa bizda <strong>Watcher</strong> lar bor. <br /><br />Watcher lar uchun ham alohida post yozamiz. Siz bilmagan va kutmagan narsalar bo&#x27;lishi mumkin. Stay tuned...<br /></p>
  <p id="akfe"><a href="https://t.me/peaceofcode" target="_blank">@peaceofcode</a></p>

]]></content:encoded></item><item><guid isPermaLink="true">https://teletype.in/@dyusupov/loading-experience-with-suspense-component</guid><link>https://teletype.in/@dyusupov/loading-experience-with-suspense-component?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=dyusupov</link><comments>https://teletype.in/@dyusupov/loading-experience-with-suspense-component?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=dyusupov#comments</comments><dc:creator>dyusupov</dc:creator><title>Vue Suspense component ⏳</title><pubDate>Thu, 18 Jul 2024 16:15:44 GMT</pubDate><media:content medium="image" url="https://img1.teletype.in/files/07/cf/07cf3d92-ba71-4032-ac4d-1e246d5e0970.png"></media:content><description><![CDATA[<img src="https://img4.teletype.in/files/7a/2f/7a2f4d57-fe0c-47b2-9a52-eb202951bd86.jpeg"></img>Kontent yuklanishini kutish (Content loading) - bu web UX ning ajralmas qismidir. Internet tezligini o'ynashi hatto eng tezkor ilovalarni ham sekinlashtirishi mumkin, bu esa foydalanuvchining o'ylash va diqqatini buzish xavfini tug'diradi.]]></description><content:encoded><![CDATA[
  <figure id="0ntz" class="m_original">
    <img src="https://img3.teletype.in/files/a3/d8/a3d883a2-698a-4250-972c-fb5e2dc3e2b0.jpeg" width="700" />
  </figure>
  <p id="Gh9J">Kontent yuklanishini kutish (Content loading) - bu web UX ning ajralmas qismidir. Internet tezligini o&#x27;ynashi hatto eng tezkor ilovalarni ham sekinlashtirishi mumkin, bu esa foydalanuvchining o&#x27;ylash va diqqatini buzish xavfini tug&#x27;diradi.</p>
  <p id="xxWZ">Yaxshi ishlab chiqilgan loading indikatorlar foydalanuvchiga to&#x27;gri feedback beradi — ularning so&#x27;rovi tizim tomonidan &quot;eshitilganini&quot; tasdiqlaydi, Loading progress ni ko&#x27;rsatadi va hatto kutilayotgan vaqt qisman kamaygandek tuyuladi. </p>
  <h3 id="47ac"></h3>
  <h2 id="SCv2">Vue 3’s Suspense component</h2>
  <p id="FCn2">SPA da ayniqsa Loading experience yaxshi bo&#x27;lmaydi. Sababi content ni yuklashda juda ko&#x27;p Fetch request lar berilgani sababli UI da Spinner ustiga spinner va har xil progress bar larga to&#x27;lib ketadi. Shu narsani oldini olish uchun Vue 3 <a href="https://vuejs.org/guide/built-ins/suspense.html" target="_blank">Suspense Component</a> ni taqdim qildi. Suspense component bizga data-fetching  qiladigan ko&#x27;p componentlarni coordinate qiladigan va Loading experience ni yaxshilaydigan yangi patternni beradi. Keling chuqurroq ko&#x27;rib chiqamiz.<br /></p>
  <h2 id="fe4b">Render, then fetch</h2>
  <p id="cwUi">Odatiy SPA application larda view ko&#x27;p componentlardan va nested componentlardan tashkil topgan bo&#x27;ladi. Va bu componentlar o&#x27;z content ini o&#x27;zi yuklashi mumkin. Suspense component siz odatiy loading pattern o&#x27;z ichiga mounted da data ni fetch qiladi, shuningdek loading va error ni ham ko&#x27;rsatadi:<br /><br /></p>
  <figure>
    <script src="https://gist.github.com/DoniyorYusupov/45ee9f82ef796ae642796fd71f18a262.js"></script>
  </figure>
  <p id="yKjX">View ni shunday componentlar bilan to&#x27;ldirsak har bir component kerakmas re-render lar yoki layout shift larga olib kelishi mumkin.</p>
  <figure id="RBil" class="m_original">
    <img src="https://img2.teletype.in/files/57/66/576660db-1e31-4bae-8499-b319a50816cb.gif" width="700" />
    <figcaption>Without Suspense Component</figcaption>
  </figure>
  <h3 id="59d1"></h3>
  <h2 id="s7Fk">Fetch, then render</h2>
  <p id="4051">Suspense esa hamma children component lardagi asinxron data fetching tugamaguncha Component tree ni render qilishni to&#x27;xtatib turadi. Componentda data-fetching borligini Suspense u component ni <code>&lt;script async setup&gt;</code> ligidan yoki <code>&lt;script setup&gt;</code> ning ichida <code>top-level await</code> borligidan biladi. E&#x27;tibor bering - Loading va Error logikasi individual component tomonidan boshqarilmayapti:</p>
  <figure>
    <script src="https://gist.github.com/DoniyorYusupov/0cadb27569e6d95d616e778d04acad24.js"></script>
  </figure>
  <p id="lxGE">Endi componentlar Viewda har xil vaqtda resolve bo&#x27;lmaydi. Suspence Component hamma componentlar o&#x27;zining async request larini tugatmagunicha fallback ni ko&#x27;rsatadi va qandaydir error chiqsa onErrorCaptured hook ni ishga tushiradi:</p>
  <figure>
    <script src="https://gist.github.com/DoniyorYusupov/6bfab3545d4a4dc8f9581bdc73ddab91.js"></script>
  </figure>
  <figure id="uyOv" class="m_column">
    <img src="https://img3.teletype.in/files/eb/80/eb80b61c-a588-4a5a-9983-cb5973da8dcc.gif" width="700" />
    <figcaption>With Suspense Component</figcaption>
  </figure>
  <p id="FTnD"></p>
  <p id="Urnb">Keling Suspense component ni bir nechta amaliy misollarda ko&#x27;rib chiqamiz.</p>
  <figure id="knta" class="m_column">
    <img src="https://img2.teletype.in/files/1c/97/1c97303d-ab15-4939-84cb-b3719624a5ce.png" width="720" />
  </figure>
  <p id="hqvw">Chap tarafda har bir component loading ni o&#x27;zi kontrol qilmoqda va content lar har xil vaqtda render bo&#x27;lishmoqda.<br />O&#x27;ng tarafda esa birgina Suspense componentini view-level ga qo&#x27;yib hamma component uchun umumiy Loading indikatorini ko&#x27;rsatib, bir vaqtda render qilishimiz mumkin.<br /></p>
  <figure id="KYHW" class="m_column">
    <iframe src="https://codepen.io/ajithranka/embed/LYOBzob"></iframe>
  </figure>
  <p id="LKwY">Error larni ham shunda global qilishni iloji bor:<br /></p>
  <figure id="xT2K" class="m_column">
    <iframe src="https://codepen.io/ajithranka/embed/MWOBGMG"></iframe>
  </figure>
  <p id="dnje"></p>
  <h2 id="RhAw">Nested Async component lar uchun Suspense componentni ishlatish</h2>
  <p id="fJoX"></p>
  <figure id="ske0" class="m_column">
    <iframe src="https://codepen.io/ajithranka/embed/XWzBYmo"></iframe>
  </figure>
  <p id="foX2">Qachonki bir necha data-fetching component lar component tree da nested bo&#x27;lsa,  sahifa keraksiz re-render lar ni amalga oshiradi. Suspense esa nested component larni ham asinxron amallarini tugatgunicha kutib turadi va content sahifada bir marotaba render boladi.</p>
  <p id="U1OH"></p>
  <h2 id="SCLn">Suspense component ni ichida nested component larni yuklanish ketma ketligini kontrol qilamiz</h2>
  <p id="7jLX"></p>
  <figure id="OAxO" class="m_column">
    <img src="https://img1.teletype.in/files/01/7a/017a7410-e9f9-48ac-a7e2-fc5d1d5940a8.png" width="720" />
  </figure>
  <p id="UfPe">Faraz qilaylik, bizda ma&#x27;lum bir component uzoq load boladi. Bu ssenariyda qolgan content ni birinchi ko&#x27;rsatib, uzoq load bo&#x27;ladigan component larni orqa tomonda yuklanishini kutamiz. Biz bunga shu component ni o&#x27;zini alohida Suspense ga o&#x27;rashimiz kerak bo&#x27;ladi.</p>
  <figure id="0kPb" class="m_column">
    <iframe src="https://codepen.io/ajithranka/embed/RwjBJbr"></iframe>
  </figure>
  <p id="91Ce">Chap tarafda bitta umumiy Suspense ga o&#x27;ralgan 2 ta tez yuklanadigan va 1 ta uzoq yuklanadigan component.<br />O&#x27;ng tarafda umumiy Suspense ga o&#x27;ralgan 2 ta tez yuklanadigan va o&#x27;zi alohida Suspense ga o&#x27;ralgan uzoq yuklanadigan component.</p>
  <p id="ZLab"></p>
  <p id="Zl8c">Hullas biz Suspense component bizga qanday qilib Loading experience ni yaxshilashga yordam berishini ko&#x27;rib chiqdik.<br /><br />Qo&#x27;shimcha savollaringiz bo&#x27;lsa sizni o&#x27;z kanalimda kutaman.<br /><br />Hurmat bilan Doniyor (<a href="https://t.me/peaceofcode" target="_blank">PeaceOfCode</a>)</p>
  <p id="eEPQ"><br /><br /><br /><br />P.s. Special thanks to <a href="https://medium.com/arcana-network-blog" target="_blank">Arcana Network Blog</a> for images and  <a href="https://codepen.io/ajithranka" target="_blank">Ajith Ranka</a> for practical examples.</p>

]]></content:encoded></item><item><guid isPermaLink="true">https://teletype.in/@dyusupov/Vue-Async-Components-07-11</guid><link>https://teletype.in/@dyusupov/Vue-Async-Components-07-11?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=dyusupov</link><comments>https://teletype.in/@dyusupov/Vue-Async-Components-07-11?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=dyusupov#comments</comments><dc:creator>dyusupov</dc:creator><title>Async Components in Vue</title><pubDate>Wed, 17 Jul 2024 04:51:19 GMT</pubDate><media:content medium="image" url="https://img3.teletype.in/files/a0/36/a036815c-92ed-4941-a1c4-a7ccd1aa394f.png"></media:content><description><![CDATA[<img src="https://img3.teletype.in/files/6b/b3/6bb326cb-2502-4d6e-867d-a041d55cbf5b.png"></img>Katta web ilovalarni dasturlashda, ularning tez yuklanishi va interaktiv elementlarining javob berish tezligi UX ga katta ta'sir ko'rsatishi mumkin. Web-ilovalar o'lchami va murakkabligi ortib borar ekan, katta hajmdagi kodlarni faqat kerak bo'lganda yuklashni ta'minlash muhim ahamiyat kasb etadi. Shu yerda Vue dagi asinxron komponentlar yordamga keladi.]]></description><content:encoded><![CDATA[
  <p id="uKM4">Katta web ilovalarni dasturlashda, ularning tez yuklanishi va interaktiv elementlarining javob berish tezligi UX ga katta ta&#x27;sir ko&#x27;rsatishi mumkin. Web-ilovalar o&#x27;lchami va murakkabligi ortib borar ekan, katta hajmdagi kodlarni faqat kerak bo&#x27;lganda yuklashni ta&#x27;minlash muhim ahamiyat kasb etadi. Shu yerda Vue dagi asinxron komponentlar yordamga keladi.</p>
  <p id="Y2Ry">Faraz qiling bizda oddiy modal component bor va parent da button bosilganda shu modal component render bo&#x27;lishi kerak.<br />Modal component o&#x27;z ichiga faqat style va modal qanday paydo bo&#x27;lishini belgilaydi.</p>
  <figure id="2NBR" class="m_column">
    <img src="https://img3.teletype.in/files/6b/b3/6bb326cb-2502-4d6e-867d-a041d55cbf5b.png" />
    <figcaption>src/components/Modal.vue</figcaption>
  </figure>
  <p id="fBtI"><strong>App</strong> componentda Button va uni bosganda reactive boolean o&#x27;zgaruvchi (<code>showModal</code>) yordamida render bo&#x27;lishi kerak bo&#x27;lgan Modal component render bo&#x27;ladi. Modal shu <strong>showModal </strong>o&#x27;zgaruvchisi orqali ko&#x27;rsatiladi/berkitiladi.</p>
  <figure id="AryS" class="m_column">
    <img src="https://img4.teletype.in/files/be/46/be463659-928d-4fe7-a5b8-a251ce1995e7.png" />
    <figcaption>src/App.vue</figcaption>
  </figure>
  <p id="N6DA">When we click the <code>Show Modal</code> button, the modal is shown on the page.<br /><code>Show Modal</code> tugmasi bosilganda sahifada modal paydo bo&#x27;ladi.</p>
  <figure id="DS3u" class="m_column">
    <img src="https://img2.teletype.in/files/91/03/91039455-98e8-4a2b-b9b9-3bf45faa43e7.png" />
    <figcaption>Boshlang&#x27;ich holati</figcaption>
  </figure>
  <figure id="4sfD" class="m_column">
    <img src="https://img2.teletype.in/files/16/8c/168cf6ac-247c-4aa9-93aa-e41c003e4d74.png" />
    <figcaption>Tugma bosilgandagi holat</figcaption>
  </figure>
  <p id="ISNc">Ko&#x27;rishingiz mumkinki Modal.vue faqat tugmani bosganimizda paydo bo&#x27;lsa ham Browser Network loglarida asosiy bundle bilan birga yuklanmoqda.</p>
  <p id="aqKb">Ho&#x27;sh biz Modal.vue ni faqat tugma bosilgandagina yuklanishini xohlasak, bizga <strong>defineAsyncComponent() </strong>funksiyasi kerak bo&#x27;ladi</p>
  <figure id="8VzX" class="m_column">
    <img src="https://img4.teletype.in/files/b4/b7/b4b78d5f-923c-40bb-a66b-e54417eee135.png" />
    <figcaption>src/components/AsyncModal.ts</figcaption>
  </figure>
  <p id="j1Z8">Endi App componentda AsyncModal ni import qilib, oddiy <strong>Modal</strong> ni o&#x27;rniga almashtirishimiz kerak.</p>
  <figure id="d66t" class="m_column">
    <img src="https://img4.teletype.in/files/39/c8/39c8da8a-76de-49ca-b07e-b3dc7434e017.png" />
    <figcaption>src/App.vue</figcaption>
  </figure>
  <p id="zKkz">Endi shu kichik o&#x27;zgarish orqali bizning modal asinxron yuklanadi. Sahifamizning boshlang&#x27;ich holatida <code>Modal</code> component asosiy bundle bilan <strong>yuklanmayotganini </strong>ko&#x27;rishimiz mumkin.</p>
  <figure id="B4Z3" class="m_column">
    <img src="https://img3.teletype.in/files/ec/88/ec886f73-b86b-47f2-afa5-af3b3f7c3675.png" />
    <figcaption>Boshlang&#x27;ich holati</figcaption>
  </figure>
  <p id="yz0c"><strong>Show Modal</strong> tugmasini bosganimizdan keyingina <strong>Modal.vue </strong>componenti yuklanmoqda.</p>
  <figure id="pdx5" class="m_column">
    <img src="https://img1.teletype.in/files/cd/5d/cd5dd374-70bf-4600-be74-a74186b23896.png" />
    <figcaption>Tugma bosilgandan keyingi holat</figcaption>
  </figure>
  <h4 id="Loading-va-Error-UI">Loading va Error UI</h4>
  <p id="TDBz">Vue bizga<strong> defineAsyncComponent() </strong>asinxron componentni yuklashdan tashqari ushbu component yuklanish vaqtidagi loading protsessini va yuzaga kelgan xatolik haqida ma&#x27;lumotni ko&#x27;rsatish imkonini beradi. Bu esa bizga o&#x27;z navbatida internetning har qanday holatida ham silliq UX ni ta&#x27;minlashga yordam beradi.</p>
  <h4 id="loadingComponent">loadingComponent</h4>
  <p id="pMtU">Tepadagi tugmani bosgan paytda Modal.vue yuklangunicha bo&#x27;lgan vaqtda UI da uzilish bo&#x27;lmasligi uchun <strong>defineAsyncComponent()</strong> bizga qo&#x27;shimcha <code>loadingComponent</code> optionini ham beradi. Undan foydalanishimiz uchun biz defineAsyncComponent ga import funksiyasini emas, object beramiz va import funksiyani <code>loader</code> option iga qo&#x27;yamiz:</p>
  <p id="eQlE">Bizda oddiy Loading component bor:</p>
  <figure id="9eDu" class="m_column">
    <img src="https://img2.teletype.in/files/d6/74/d6747992-9d15-4c66-b562-c283c63b90a5.png" />
    <figcaption>src/components/Loading.vue</figcaption>
  </figure>
  <p id="UdHW">Biz uni <strong>loadingComponent</strong> optioniga beramiz</p>
  <figure id="tOSm" class="m_column">
    <img src="https://img4.teletype.in/files/37/a8/37a87d47-84c8-4117-8a46-3a94e00f5077.png" />
    <figcaption>src/components/AsyncModal.vue</figcaption>
  </figure>
  <p id="S9Vs">Loading ni ko&#x27;rish uchun Network da internet tezligini Slow 3G ga o&#x27;tkazib sekin internet holatini sun&#x27;iy ko&#x27;rinishda yuzaga keltiramiz (Bo&#x27;lmasam Loading... chiqishini ko&#x27;rishni iloji yo&#x27;q).</p>
  <figure id="82zL" class="m_column">
    <img src="https://img1.teletype.in/files/ce/7c/ce7c841e-6413-444c-b93c-17f2161ed26a.png" />
    <figcaption>Tugma bosilgandan keyingi holat</figcaption>
  </figure>
  <p id="eKpP">Browser Modal.vue ni yuklab bo&#x27;lgach uni Loading component bilan almashtiradi.</p>
  <figure id="JgJb" class="m_column">
    <img src="https://img4.teletype.in/files/38/85/388547f6-2c49-4e7f-99e7-da13a75070fd.png" />
    <figcaption>Yuklab bo&#x27;lganidan keyingi holat</figcaption>
  </figure>
  <h4 id="errorComponent">errorComponent</h4>
  <p id="mr2I">Huddi loadingComponent kabi defineAsyncComponent() bizga errorComponent optionini beradi. Keling oddiy Error.vue componentini yasaymiz:</p>
  <figure id="jlk7" class="m_column">
    <img src="https://img4.teletype.in/files/f3/46/f3467c50-41da-4b6a-9ef9-ea6b1ba82395.png" />
    <figcaption>src/components/Error.vue</figcaption>
  </figure>
  <p id="wZuh">Endi uni errorComponent optioniga beramiz</p>
  <figure id="VSG7" class="m_column">
    <img src="https://img2.teletype.in/files/d1/ff/d1ff0eb3-d5f0-4f2e-b313-c99416d7fe95.png" />
    <figcaption>src/components/AsyncModal.ts</figcaption>
  </figure>
  <p id="62Ru">Error component ni ko&#x27;rish uchun browser ni Network tabida internet ni Offline qilish kerak bo&#x27;ladi.</p>
  <figure id="KE9G" class="m_column">
    <img src="https://img1.teletype.in/files/c3/7d/c37d4d4e-90e0-4eb0-adca-702ed5567227.png" />
  </figure>
  <p id="tvfU"><strong>defineAsyncComponent() </strong>yana bir qancha optionlar ni taqdim etadi: <code>delay</code>, <code>timeout</code>, <code>suspensible</code> va <code>onError()</code>. Bu optionlar dasturchiga componentni asinxron yuklash va UX ni kontrol qilishda ko&#x27;p imkoniyatlarni beradi. Ko&#x27;proq detallar haqida <a href="https://vuejs.org/api/general.html#defineasynccomponent" target="_blank">API documentation</a> ga murojaat qilishingizni so&#x27;rayman.</p>
  <p id="hPG3">Darsligimiz yoqdi degan umiddamiz. <a href="https://t.me/peaceofcode" target="_blank">https://t.me/peaceofcode</a> ni kuzatib boring, yanada ko&#x27;proq mavzularni O&#x27;zbek tilida yoritib borishga harakat qilamiz. 🫰</p>
  <p id="F0Wa">Hurmat bilan, Doniyor.</p>

]]></content:encoded></item></channel></rss>