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.

Using Context breaks components with setState in React Native 0.54

See original GitHub issue

React Native uses React 16.3.0-alpha.1 which has the new context API. However, there’s a bug in the context API which makes the consumer re-render its children every time one of its children call setState: https://github.com/facebook/react/issues/12218

This breaks components like FlatList which call setState internally where you get a stack overflow due to re-render happening thousands of times. The workaround is to wrap FlatList with a PureComponent to short-circuit the updates.

It seems to have been fixed here: https://github.com/facebook/react/pull/12254, so we would need to update the React version used by React Native once it’s released.

Environment

Environment: OS: macOS High Sierra 10.13.4 Node: 9.5.0 Yarn: 1.3.2 npm: 5.6.0 Watchman: 4.9.0 Xcode: Xcode 9.2 Build version 9C40b Android Studio: 2.3 AI-162.4069837

Packages: (wanted => installed) react: ^16.3.0-alpha.1 => 16.3.0-alpha.1 react-native: 0.54.2 => 0.54.2

Expected Behavior

Should not cause a stack overflow.

Actual Behavior

Causes a stack overflow.

Steps to Reproduce

It’s not possible to provide a Snack since it uses an older version of React Native. You can create a new project with react-native init and copy the following code to App.js:

import * as React from 'react';
import { Text, FlatList } from 'react-native';

const { Consumer } = React.createContext({});

export default class App extends React.Component {
  render() {
    return (
      <Consumer>
        {() => {
          console.log('re-rendered');

          return (
            <FlatList
              data={Array.from({ length: 100 }).map((_, i) => `Item ${1}`)}
              renderItem={({ item }) => <Text>{item}</Text>}
              keyExtractor={(item, index) => 'key' + index}
            />
          );
        }}
      </Consumer>
    );
  }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
hramoscommented, Mar 14, 2018

The React <-> React Native sync has landed in https://github.com/facebook/react-native/commit/4f8328bf2fb71b104baa70178a1e1f9636773c6a. Can you take a look and see if this still happens?

0reactions
hramoscommented, Jul 16, 2018

0.56 is the latest version, and it has this fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks API Reference
The setState function is used to update the state. It accepts a new state value and enqueues a re-render of the component. setState(newState);....
Read more >
javascript - React context not updating
Think about React context just like you would a component, if you want to update a value and show it then you need...
Read more >
How to manage state in a React app with just Context and ...
setState in class components); useContext — This hook takes in a context object and returns whatever is passed in as a value prop...
Read more >
Global state with React
React context suffers from a common problem: useContext hook will re-render whenever your context is modified. In other words, every component subscribed to...
Read more >
Update state from child to parent with React Context
React Context provides a way to pass data through the component tree without having to pass props down manually at every level ☘️...
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