
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
react-dividable-context
Advanced tools
An efficient way of having one React Context instead of multiple providers, without unnecessary re-rendering
Demo available on GitHub Pages.
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
type EqualsFunction<T> = (a: T, b: T) => boolean;
// Create a new context
const context = create(defaultValue: T, EqualsFunction<T>);
// The provider/consumer, similar to the ones from React Context
const provider = <context.Provider/>; // See note below!
const consumer = <context.Consumer/>;
// A consumer that only updates if the given field(s) update
const partialConsumer = context.getConsumer(['fieldA', 'fieldB']);
// Otherwise, we can also use the "main" one with the "keys" property
const listenToFieldA = <context.Consumer keys={['fieldA']} />;
// We can mix a partial and the "keys" prop too
// This one will listen to fieldA, fieldB and fieldC
const listenToSeveral = <partialConsumer keys={['fieldC']} />;
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.
const context = create({});
// ...
<context.provider value={{}}>
<p>This will never update! {Date.now()}</p>
<context.consumer>
{data => <p>This one updates, though. {Date.now()}</p>}
</context.consumer>
</context.provider>
// ...
npm install --save react-dividable-context;
# or
yarn add react-dividable-context;
import * as React from 'react';
import create from 'react-dividable-context';
// Our demo data
interface ITest {
abc: string;
def: number;
idk: number[];
}
// Create the context, similar to React.createContext
const contexter = create({ abc: 'ABC', def: 123, idk: [0] });
const ConsumerABC = contexter.getConsumer(['abc']);
const ConsumerMixed = contexter.getConsumer(['abc', 'def']);
interface IAppState {
test: ITest;
}
class App extends React.Component<any, IAppState> {
constructor(props: any, context?: any) {
super(props, context);
this.state = { test: { abc: 'ABC', def: 123, idk: [0] } };
}
public render() {
return (
<context.provider value={this.state.test}>
<context.consumer>
{test => <p>Something changed! {Date.now()}</p>}
</context.consumer>
<ConsumerABC>
{test => <p>Field 'abc' changed! {Date.now()}</p>}
</ConsumerABC>
<ConsumerMixed>
{test => <p>Field 'abc' and/or 'def' changed! {Date.now()}</p>}
</ConsumerMixed>
</context.provider>
)
}
}
FAQs
An efficient way of having one React Context instead of multiple providers, without unnecessary re-rendering
The npm package react-dividable-context receives a total of 1 weekly downloads. As such, react-dividable-context popularity was classified as not popular.
We found that react-dividable-context demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.