react-dividable-context
Demo available on GitHub Pages.
API
Similar to the official context API added in React 16.3, which is what gets used by this module internally.
// Check whether two values are the same// Uses `JSON.stringify(a) === JSON.stringify(b)` by default; // Create a new context; // The provider/consumer, similar to the ones from React Context; // See note below!; // A consumer that only updates if the given field(s) update; // Otherwise, we can also use the "main" one with the "keys" property; // We can mix a partial and the "keys" prop too// This one will listen to fieldA, fieldB and fieldC; // We can also check for "sub keys"// This'll listen to fieldA and fieldB.subA;
Important: Unlike the official Provider, the one exposed by context prevents its children from updating. This allows wrapping your app in the provider and updating the value
prop, without it having re-rendering the whole app. All (descendant) consumers will still update.
;// ... context.provider value= pThis will never update!</p> context.consumer /context.consumer /context.provider// ...
Usage
npm install --save react-dividable-context;# or yarn add react-dividable-context;
;; // Our demo data // Create the context, similar to React.createContext;;;