Git Product home page Git Product logo

react-context-problem's Introduction

react-context-problem

There's an issue with React's Context which causes any Consumer of a particular Provider to automatically rerender, regardless of whether or not the Component that is rerendering is interested in the value which changed.

So, if for instance you have a Provider with a value of {a: 1, b: 2}, and then you have two children of the Provider, ComponentA and ComponentB, which are interested in "a" and "b" respectively, then both ComponentA and ComponentB will rerender every time either one of these object values changes. In short, there's no way to surgically target your rerenders to one of the Provider's values.

In this repository is a running example of this, based off an article I read here. I've altered this to add useReducer to it to see if that would affect it at all, since useReducer's dispatch signature never changes. It didn't fix the issue.

I'm going to use this as a base for research to find as simple a fix as possible, either by writing a new pattern to deal with it or by finding an existing library dependency to include.

Barring a good fix, this may finally be a reason to fall back to Redux or Mobx. Global state is a good thing. We shouldn't be splitting our Contexts up into multiple pieces because that forces us to make early decisions about what Components our state affects, and that always changes as an application grows.

react-context-problem's People

Contributors

wsrast avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.