Skip to content
FrontDepth, Home

Back to the topic

Practice: State management and external stores

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

3 predict-output · 4 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    This model dispatches like React does: it skips the update when the reducer returns an Object.is-equal state. What does it log?

    JavaScript
    let state = { items: [] };
    let renders = 0;
    
    function dispatch(reducer, action) {
      const next = reducer(state, action);
      if (Object.is(next, state)) return;
      state = next;
      renders += 1;
    }
    
    function mutatingReducer(current, action) {
      current.items.push(action.item);
      return current;
    }
    
    function immutableReducer(current, action) {
      return { ...current, items: [...current.items, action.item] };
    }
    
    const before = state;
    dispatch(mutatingReducer, { item: "milk" });
    console.log(state === before);
    console.log(state.items.length);
    console.log(renders);
    
    dispatch(immutableReducer, { item: "bread" });
    console.log(state === before);
    console.log(before.items.length);
    console.log(renders);
  2. Open questionJunior

    What kinds of state does a React app have, and where should each one live?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  3. Predict the outputMiddle

    A small Redux-style store notifies subscribers; each "component" re-renders only when its selected value changes by Object.is. What does this code log?

    JavaScript
    function createStore(reducer, initialState) {
      let state = initialState;
      const listeners = new Set();
      return {
        getState: () => state,
        dispatch(action) {
          const next = reducer(state, action);
          if (Object.is(next, state)) return;
          state = next;
          listeners.forEach((listener) => listener());
        },
        subscribe(listener) {
          listeners.add(listener);
          return () => listeners.delete(listener);
        },
      };
    }
    
    function reducer(state, action) {
      switch (action.type) {
        case "cart/added":
          return { ...state, cart: [...state.cart, action.id] };
        case "theme/set":
          return action.theme === state.theme ? state : { ...state, theme: action.theme };
        default:
          return state;
      }
    }
    
    function watch(store, name, selector) {
      let selected = selector(store.getState());
      store.subscribe(() => {
        const next = selector(store.getState());
        if (!Object.is(next, selected)) {
          selected = next;
          console.log(name + " re-renders");
        }
      });
    }
    
    const store = createStore(reducer, { theme: "light", cart: [] });
    watch(store, "ThemeToggle", (s) => s.theme);
    watch(store, "CartBadge", (s) => s.cart.length);
    watch(store, "CartSummary", (s) => ({ count: s.cart.length }));
    
    store.dispatch({ type: "cart/added", id: "fern" });
    store.dispatch({ type: "theme/set", theme: "light" });
    store.dispatch({ type: "theme/set", theme: "dark" });
    store.dispatch({ type: "unknown" });
    console.log("cart: " + store.getState().cart.length);
  4. Open questionMiddle

    Why can context cause many unnecessary re-renders, and how do you fix it?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  5. Predict the outputSenior

    Three snapshot functions read the same store, as getSnapshot would for useSyncExternalStore. What does the code log?

    JavaScript
    let state = { todos: ["a", "b"], filter: "all" };
    
    const selectSummary = () => ({ count: state.todos.length });
    
    let lastState = null;
    let lastSummary = null;
    const selectSummaryCached = () => {
      if (state !== lastState) {
        lastState = state;
        lastSummary = { count: state.todos.length };
      }
      return lastSummary;
    };
    
    const selectCount = () => state.todos.length;
    
    console.log(Object.is(selectSummary(), selectSummary()));
    console.log(Object.is(selectSummaryCached(), selectSummaryCached()));
    
    const summaryBefore = selectSummaryCached();
    const countBefore = selectCount();
    state = { ...state, filter: "done" };
    
    console.log(Object.is(summaryBefore, selectSummaryCached()));
    console.log(Object.is(countBefore, selectCount()));
    console.log(selectSummaryCached().count === summaryBefore.count);
  6. Open questionSenior

    Why does useSyncExternalStore exist, and what does it require from a store?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  7. Open questionSenior

    Why is server state different from client state, and what does a library like TanStack Query do about it?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.