Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
¿Cómo evitar volver a renderizar en reaccionar nativo?

Estoy tratando de evitar volver a renderizar componentes innecesarios
Por ejemplo:

 const[ValueState,SetValueState]=useState(5); //hook <View> <Text>{ValueState}</Text> <View> { [...Array(3)].map((index,el)=>{ return (<View><Text>Hello there</Text></View>) }) } </View> </View>

aquí, cada vez que cambio el valor de ValueState el segmento completo del mapa () también se vuelve a representar
¿Cómo evito esto y hago que el segmento map() solo se represente 1 vez?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Depende de qué depende la función que no desea volver a renderizar. En este caso, su matriz y su función de mapa no dependen directamente de ValueState .

Una forma de lograr 1 renderizado es usando React.memo

Ejemplo para representar la función de mapa solo una vez

 import React, { useState } from "react"; import { View, Text, TouchableOpacity } from "react-native"; const ArrayMapSection = React.memo(()=> { console.log("ArrayMapSection rendered") return [...Array(3)].map((index,el)=>{ return (<View><Text>Hello there</Text></View>) }); }) const App = () => { const [ValueState,SetValueState]=useState(5); //hook return( <View> <Text>{ValueState}</Text> <TouchableOpacity onPress={()=>SetValueState(Math.random())}>Press to state update</TouchableOpacity> <View> <ArrayMapSection /> </View> </View> ) }; export default App;

Si ejecuta este programa, verá que ArrayMapSection rendered solo una vez en la consola. Ahora intente cambiar el ValueState presionando Press to state update . ArrayMapSection no se volverá a renderizar porque React.memo solo se vuelve a renderizar si los accesorios cambian

Más información: https://reactjs.org/docs/react-api.html#reactmemo

about 4 years ago · Juan Pablo Isaza Report

0

Cree un componente de reacción personalizado que tome la matriz como accesorio y la asigne a otros componentes.

De esa manera, el componente solo se vuelve a renderizar si cambia la propiedad de la matriz.

Ejemplo de código:

 const[ValueState,SetValueState]=useState(5); <View> <Text>{ValueState}</Text> <CustomList array={[1,2,3]} /> </View> export const CustomList = (array) => { return ( <> { array.map((index,el)=>{ return (<View><Text>Hello there</Text></View>) }) } <\> ) }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!