question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hooks and Flatlist Bug in Update State

See original GitHub issue

🐛 Bug Report

To Reproduce

Expected Behavior

I am using Hooks in React-Native. I got a problem with my state update, when using FlatList. If I call a function of the parent inside a rendered item of my flatlist to change a state, the state isn’t updated. Although useEffect says that its updated, after the next function call, my state is resetted.

Code Example

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import { FlatList, Text, TouchableOpacity, View } from 'react-native';
import React, { useEffect, useState } from 'react';

import NavigationHandler from '../../hoc/navigationHandler/NavigationHandler';
import OCRImageView from '../../components/main/ocrImageView/OCRImageView';

const data = [1, 2, 3, 4];
const MainScreen = () => {
  const [array, setArray] = useState([]);
  const [counter, setCounter] = useState(0);
  useEffect(() => {
    console.log('array: ', array);
  }, [array]);
  const onPressAdd = () => {
    setArray([...array, counter]);
    setCounter(counter + 1);
  };
  const onPressRemove = () => {};
  const _keyExtractor = (item, index) => index.toString();
  const _renderItem = ({ item }) => {
    return (
      <ChildComponent onPressAdd={onPressAdd} onPressRemove={onPressRemove} />
    );
  };
  return (
    <View>
      <FlatList
        keyExtractor={_keyExtractor}
        data={data}
        renderItem={_renderItem}
      />
    </View>
  );
};

export default MainScreen;

type Props = {
  onPressAdd: () => void,
  onPressRemove: () => void
};
const ChildComponent = ({ onPressAdd, onPressRemove }: Props) => {
  return (
    <View>
      <TouchableOpacity onPress={onPressAdd}>
        <Text>add</Text>
      </TouchableOpacity>
      <TouchableOpacity onPress={onPressRemove}>
        <Text>remove</Text>
      </TouchableOpacity>
    </View>
  );
};

Environment

React Native Environment Info: System: OS: macOS High Sierra 10.13.6 CPU: (4) x64 Intel® Core™ i5-2400S CPU @ 2.50GHz Memory: 106.48 MB / 14.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 11.9.0 - /usr/local/bin/node Yarn: 1.13.0 - /usr/local/bin/yarn npm: 6.5.0 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman SDKs: iOS SDK: Platforms: iOS 11.2, macOS 10.13, tvOS 11.2, watchOS 4.2 Android SDK: API Levels: 23, 25, 26, 27, 28 Build Tools: 23.0.1, 26.0.1, 26.0.3, 27.0.3, 28.0.3 System Images: android-27 | Google Play Intel x86 Atom, android-28 | Google APIs Intel x86 Atom IDEs: Android Studio: 3.2 AI-181.5540.7.32.5056338 Xcode: 9.2/9C40b - /usr/bin/xcodebuild npmPackages: react: 16.8.3 => 16.8.6 react-native: 0.59.1 => 0.59.1 npmGlobalPackages: react-native-cli: 2.0.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
AntoineDoubovetzkycommented, Apr 14, 2019

@karvulf The FlatList doesn’t rerender because the data passed to the FlatList are the same. You have to add the extraData prop to the FlatList (https://facebook.github.io/react-native/docs/flatlist#extradata)

0reactions
stale[bot]commented, Oct 31, 2019

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating FlatList data with useState infinitely recurs in React ...
I'm trying to set the data for a flatlist with useState, but updating the state causes the component to re-render, so it calls...
Read more >
Optimizing Flatlist Configuration - React Native
Cons: Be aware that this implementation can have bugs, such as missing content (mainly observed on iOS), especially if you are doing complex ......
Read more >
How to manage the useEffect dependency array like a pro?
React useEffect hooks are powerful to run side effects, but it is easy to omit dependencies and create bugs in your app. You...
Read more >
How to re-render react native FlatList when State is changed
This is a quick video to show you how to re-render your FlatList component when you change the state. It is very tricky...
Read more >
Should I use useEffect to reload flatlist every time data ... - Reddit
Why would you need to use useEffect to reload the flatlist? If the data updates the state, the flatList should be updated automatically....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found