Skip to content
TheChatArchiveThe Chat Archive
Qwenqwq-32b· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

mam taki kod: function useCursorDrop(enabled: MaybeRefOrGetter<boolean>) { const…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Kimikimi-k2-0711-preview’s answer.
  1. User

    mam taki kod: function useCursorDrop(enabled: MaybeRefOrGetter) { const isCursorDrop = ref(false); const isEnabled = computed(() => enabled);

    function showCursorDrop() { console.log("showCursorDrop->", isEnabled.value); if (isEnabled.value) { isCursorDrop.value = true; } } function hideCursorDrop() { if (isEnabled.value) { isCursorDrop.value = false; } }

    return { isCursorDrop: readonly(isCursorDrop), showCursorDrop, hideCursorDrop, }; }

    i mam takie użycie const { showCursorDrop, isCursorDrop, hideCursorDrop } = useCursorDrop( controlsGUIStore.controls.cursorDropEnabled, ); function onPointerDown() { showCursorDrop(); }

    function onPointerUp() { hideCursorDrop(); } function raycast() { if (isCursorDrop.value) { const uniforms = heightmapVariable.material.uniforms; raycaster.value.setFromCamera(mouseCoords, camera.value!); const intersects = raycaster.value.intersectObject(meshRay.value);

    if (intersects.length > 0) {
      const point = intersects[0].point;
      dropManager.value.updateDropOn(0, {
        x: point.x,
        y: point.z,
        size: controlsGUIStore.controls.cursorDropSize,
        depth: controlsGUIStore.controls.cursorDropDepth,
      });
    
      dropManager.value.updateUniforms(uniforms);
    }
    

    } }

    Dlaczego zmiana controlsGUIStore.controls.cursorDropEnabled nie prowadzi do ponownego obliczenia przez computed?

  2. Qwen Assistant

    Problem, który występuje, polega na tym, że computed isEnabled nie śledzi zmian w controlsGUIStore.controls.cursorDropEnabled, ponieważ parametr enabled nie jest poprawnie odsyłaczem reaktywnym. Oto wyjaśnienie i rozwiązanie:


    Problem

    W kodzie funkcji useCursorDrop computed isEnabled jest zdefiniowany jako:

    const isEnabled = computed(() => enabled);
    

    Gdy enabled jest:

    1. Wartością stałą (nie reaktywna) → computed nigdy się nie odświeży.
    2. Właściwością z obiektu reaktywnego (np. controlsGUIStore.controls.cursorDropEnabled) → computed nie śledzi zmian, ponieważ przekazano wartość, a nie referencję do właściwości.

    Rozwiązanie

    Aby computed isEnabled śledził zmiany w controlsGUIStore.controls.cursorDropEnabled, musisz:

    1. Przekazać funkcję getter lub referencję reaktywną (np. ref) do useCursorDrop.
    2. Poprawnie odsyłać parametr enabled w funkcji useCursorDrop.

    Krok 1: Modyfikacja użycia useCursorDrop

    Zamiast przekazywać wartość bezpośrednio:

    // ZŁE: Przekazuje wartość, a nie referencję
    const { ... } = useCursorDrop(controlsGUIStore.controls.cursorDropEnabled);
    

    Przekazuj funkcję getter, która pobiera wartość z reaktywnego źródła:

    // DOBRE: Przekazuje funkcję getter
    const { ... } = useCursorDrop(() => controlsGUIStore.controls.cursorDropEnabled);
    

    Lub jeśli controlsGUIStore.controls jest obiektem reaktywnym, możesz użyć toRef:

    import { toRef } from 'vue';
    
    // DOBRE: Utwórz reaktywną referencję
    const cursorDropEnabledRef = toRef(controlsGUIStore.controls, 'cursorDropEnabled');
    const { ... } = useCursorDrop(cursorDropEnabledRef);
    

    Krok 2: Popraw kod w useCursorDrop

    Upewnij się, że computed isEnabled poprawnie odsyła parametr enabled:

    function useCursorDrop(enabled: MaybeRefOrGetter<boolean>) {
      const isEnabled = computed(() => {
        if (isFunction(enabled)) {
          return enabled(); // Wywołaj funkcję getter
        } else if (isRef(enabled)) {
          return enabled.value; // Pobierz wartość z ref
        } else {
          return enabled; // Wartość stała
        }
      });
    
      // Reszta kodu...
    }
    

    Dlaczego to działa?

    1. Funkcja getter (() => controlsGUIStore.controls.cursorDropEnabled) zawsze zwraca bieżącą wartość z reaktywnego źródła.
    2. Computed isEnabled śledzi zmiany w enabled poprzez wywoływanie funkcji getter lub odczyt ref.value.
    3. Gdy controlsGUIStore.controls.cursorDropEnabled ulegnie zmianie, computed odświeży się automatycznie.

    Podsumowanie

    • Błąd polegał na przekazaniu wartości zamiast reaktywnej referencji.
    • Rozwiązanie to przekazanie funkcji getter lub ref, aby computed mogło śledzić zmiany.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.