Əsas məzmuna keç
FrontDepth, Ana səhifə

Mövzuya qayıt

Məşq: Reconciliation və re-render

Əvvəlcə cavab ver, sonra müqayisə et. Özünü dürüst qiymətləndir — zəif yerlərin siyahısı buna əsasən qurulur.

4 nəticə sualı · 3 açıq sual

Sualların səviyyəsi

Seçilmiş səviyyəyə qədər olan suallar göstərilir.

Bu səviyyə üçün hələ məşq sualı yoxdur.

  1. Nəticəni təxmin etJunior

    Bu adi JS modeli (React-siz) React-in default davranışını təqlid edir: komponenti render etmək onu çağırmaq deməkdir, komponent isə qaytardığı komponentləri render edir. İlk render-də, Counter-in state-i dəyişəndə və App eyni props ilə yenidən render olunanda konsola nə çıxacaq?

    JavaScript
    const calls = [];
    
    function render(component, props) {
      calls.push(component.name);
      component(props);
    }
    
    function App({ theme }) {
      render(Header, { theme });
      render(Counter, { step: 1 });
      render(Footer, {});
    }
    function Header() {}
    function Counter() {
      render(Display, {});
    }
    function Display() {}
    function Footer() {}
    
    render(App, { theme: "light" });
    console.log(calls.join(" "));
    
    calls.length = 0;
    render(Counter, { step: 1 });
    console.log(calls.join(" "));
    
    calls.length = 0;
    render(App, { theme: "light" });
    console.log(calls.length);
  2. Açıq sualJunior

    React-də "render" nə deməkdir və komponentin yenidən render olunmasına nə səbəb olur?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.

  3. Nəticəni təxmin etMiddle

    Bu model (React-siz) React-in eyni elementlərdə etdiyi bailout-u təqlid edir: slot əvvəlki dəfəki eyni element obyektini alanda ötürülür. Layout Feed-i children kimi alır. İlk render-də, Layout-un öz state-i dəyişəndə (update("root/0")) və App-in state-i dəyişəndə (update("root")) nə icra olunur?

    JavaScript
    const calls = [];
    const tree = new Map();
    const h = (type, props = {}) => ({ type, props });
    
    function render(path, element) {
      if (tree.get(path) === element) return;
      tree.set(path, element);
      update(path);
    }
    
    function update(path) {
      const { type, props } = tree.get(path);
      calls.push(type.name);
      type(props).forEach((child, i) => render(path + "/" + i, child));
    }
    
    function Sidebar() {
      return [];
    }
    function Post() {
      return [];
    }
    function Feed() {
      return [h(Post), h(Post)];
    }
    function Layout({ children }) {
      return [h(Sidebar), children];
    }
    function App() {
      return [h(Layout, { children: h(Feed) })];
    }
    
    render("root", h(App));
    console.log(calls.join(" "));
    
    calls.length = 0;
    update("root/0");
    console.log(calls.join(" "));
    
    calls.length = 0;
    update("root");
    console.log(calls.join(" "));
  4. Nəticəni təxmin etMiddle

    Key-li reconciliation-ın bu modeli (React-siz) key-i yenidən tapılan sətri draft-ı ilə birlikdə — idarə olunmayan input-dakı mətn kimi — təkrar istifadə edir, yeni key üçün isə sətir yaradır (* ilə işarələnir). İstifadəçi Milk və Bread üçün qeyd yazıb, sonra siyahının başına Eggs əlavə olunur. İndeks key-ləri və id key-ləri nə verir?

    JavaScript
    function reconcile(oldRows, items, keyOf) {
      const byKey = new Map(oldRows.map((row) => [row.key, row]));
      return items.map((item, index) => {
        const key = keyOf(item, index);
        const row = byKey.get(key) ?? { key, draft: "", created: true };
        row.label = item.name;
        return row;
      });
    }
    
    function show(rows) {
      return rows.map((row) => row.label + "=" + row.draft + (row.created ? "*" : "")).join(" | ");
    }
    
    const before = [
      { id: "a", name: "Milk" },
      { id: "b", name: "Bread" },
    ];
    const after = [{ id: "c", name: "Eggs" }, ...before];
    
    for (const keyOf of [(item, index) => index, (item) => item.id]) {
      const rows = reconcile([], before, keyOf);
      rows[0].draft = "2 l";
      rows[1].draft = "whole";
      for (const row of rows) row.created = false;
      console.log(show(reconcile(rows, after, keyOf)));
    }
  5. Açıq sualMiddle

    React-ə siyahılarda key-lər niyə lazımdır və indeks və ya təsadüfi key-lərlə nə səhv gedir?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.

  6. Nəticəni təxmin etSenior

    Batching-in bu modelində (React-siz) setState əməliyyatı növbəyə qoyur və bir microtask-da tək flush planlaşdırır. Konsola nə çıxacaq?

    JavaScript
    let state = 0;
    let queue = [];
    let scheduled = false;
    let renders = 0;
    
    function setState(action) {
      queue.push(action);
      if (!scheduled) {
        scheduled = true;
        queueMicrotask(flush);
      }
    }
    
    function flush() {
      scheduled = false;
      for (const action of queue) state = typeof action === "function" ? action(state) : action;
      queue = [];
      renders += 1;
      console.log("render " + renders + ": " + state);
    }
    
    function handleClick() {
      const count = state;
      setState(count + 1);
      setState(count + 1);
      setState((c) => c + 1);
      console.log("in handler: " + state);
    }
    
    handleClick();
    setTimeout(() => {
      setState((c) => c + 10);
      setState(100);
      setState((c) => c * 2);
      console.log("in timeout: " + state);
    }, 0);
  7. Açıq sualSenior

    Axtarış qutusuna yazmaq böyük React səhifəsini ləngidir. Səbəbi necə tapırsan və işi necə azaldırsan?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.