Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
How does these line manipulate a state variable with an array in React?

I'm sitting down to visualize the quick sort algorithm with React.js for university. After a bit of searching, I found this code for partitioning, which is also intended for React.

async function partition(arr, start, end) {
        // Taking the last element as the pivot
        const pivotValue = arr[end];
        let pivotIndex = start;

        await changeColorForPivot(end)


        for (let i = start; i < end; i++) {
            if (arr[i] < pivotValue) {
                // Swapping elements
                [arr[i], arr[pivotIndex]] = [arr[pivotIndex], arr[i]];
                // Moving to next element
                pivotIndex++;
            }
        }

        // Putting the pivot value in the middle
        [arr[pivotIndex], arr[end]] = [arr[end], arr[pivotIndex]]

        await changeColorForPivot(pivotIndex)

        return pivotIndex;
    }

My array that I want to sort is in a state variable so that I can react to a change for display it visually.

const [arrForVisualisation, setArrForVisualisation] = useState([])

My question now is what exactly these two lines do in the Partition function

// Swapping elements
[arr[i], arr[pivotIndex]] = [arr[pivotIndex], arr[i]];

// Putting the pivot value in the middle
[arr[pivotIndex], arr[end]] = [arr[end], arr[pivotIndex]]

To me it seems like these two lines of code manipulate the state variable, as if they also update/change it at the same time. To my knowledge, I always thought that you can update/change a state variable only with the corresponding setState() function.

For my further proceeding it would help me very much to understand these two lines of code, since I must still adapt the function somewhat, in order to be able to visualize the happening changes accordingly.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda