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

156
Views
React Native useState prevent rerendering flatlist

So I have a React Native Flatlist. Then I have a useState which fires when I click on a item in my flatlist. The problem I am facing to right now is that if I click on a item and the useState() fires every item is going to rerender. This is bad because it makes the app extremly slow.

Is there any way to set my State without the FlatList gets rerendered but another view appears (because of the useState())??

My code looks like this:

import React, {useState} from "react"
import {View, Flatlist, Text} from "react-native"

export default const App = () => {

  const [x, setX] = useState(false)

  return(
    <View>
      <FlatList
        data = {
          ["-",
          "--",
          "---",
          "----",
          "-----",
          "------",
          "-------",
          "--------",
          "---------"]
        }
        renderItem={({item}) => {(
          <View>
            <TouchableOpacity onPress={() => setX(true)}>
              <Text style={{padding: 10,}}>{item}</Text>
            </TouchableOpacity>
          </View>
        )}}
      />
      {x == true ? <Button title="It works" onPress={() => console.log("Everything is working")} /> : <View />}
    </View>
)
}

In this example the data list is very small. If it get's bigger you really can see a difference

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

0

Here are things to consider to make sure that the FlatList won't re-render on state change (see code below):

  1. Make sure that the data being passed is stored or memoized somehow.
  2. Make sure that the callbacks are also memoized.
import React, {useState} from "react"
import {View, Flatlist, Text} from "react-native"

export default const App = () => {
  // See 1. above
  const [data, setData] = useState([
    "-",
    "--",
    "---",
    "----",
    "-----",
    "------",
    "-------",
    "--------",
    "---------",
  ]);
  const [x, setX] = useState(false)

  // See 2. above
  const renderItem = useCallback(({ item }) => (
    <View>
      <TouchableOpacity onPress={() => setX(true)}>
        <Text style={{padding: 10,}}>{item}</Text>
      </TouchableOpacity>
    </View>
  ), []);

  return(
    <View>
      <FlatList
        data={data}
        renderItem={renderItem}
      />
      {x == true ? <Button title="It works" onPress={() => console.log("Everything is working")} /> : <View />}
    </View>
)
}
about 4 years ago · Juan Pablo Isaza Report

0

Here's an idea with memo and separate components:

const App = () => {
  const [x,setX] = useState(false);

  return (
    <View>
      <MyFlatList setX={setX}/>
      {x == true ? <Button title="It works" onPress={() => console.log("Everything is working")} /> : <View />}
    </View>
  )
}

and then:

const MyFlatList = React.memo(({setX}) => {
  return (
      <FlatList
        data = {
          ["-",
          "--",
          "---",
          "----",
          "-----",
          "------",
          "-------",
          "--------",
          "---------"]
        }
        renderItem={({item}) => {(
          <View>
            <TouchableOpacity onPress={() => setX(true)}>
              <Text style={{padding: 10,}}>{item}</Text>
            </TouchableOpacity>
          </View>
        )}}
      />
  );
});

crawler's answer is very good with recommendations of memoizing the data and the callback, as well.

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!