
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
react-context-composer
Advanced tools
React 16.3 shipped a new Context API. The API encourages composition. This utility component helps keep your code clean when your component will be rendering multiple Context Providers and Consumers.
npm install --save react-context-composer
import React, { Component } from 'react'
import ContextComposer from 'react-context-composer';
import createReactContext, { type Context } from 'create-react-context';
type Theme = 'light' | 'dark';
type Language = 'en';
// Pass a default theme to ensure type correctness
const ThemeContext: Context<Theme> = createReactContext('light');
const LanguageContext: Context<Language> = createReactContext('en');
class ThemeToggler extends React.Component<
{ children: Node },
{ theme: Theme, language: Language }
> {
state = { theme: 'light', language: 'en' };
render() {
return (
// Pass the current context value to the Provider's `value` prop.
// Changes are detected using strict comparison (Object.is)
<ContextComposer contexts={[
<ThemeContext.Provider value={this.state.theme} />
<LanguageContext.Provider value={this.state.language} />
]}>
<button
onClick={() => {
this.setState(state => ({
theme: state.theme === 'light' ? 'dark' : 'light'
}));
}}
>
Toggle theme
</button>
{this.props.children}
</ContextComposer>
);
}
}
function Title({children}) {
return (
<ContextComposer contexts={[ThemeContext, LanguageContext]}>
{(theme, language) => (
<h1 style={{ color: theme === 'light' ? '#000' : '#fff' }}>
{language}: {children}
</h1>
)}
</ContextComposer>
);
}
render(
<ThemeToggler>
<Title>Hi!</Title>
</ThemeToggler>,
domNode
);
MIT © Blaine Kasten
FAQs
Clean composition for Reacts new context API
The npm package react-context-composer receives a total of 228 weekly downloads. As such, react-context-composer popularity was classified as not popular.
We found that react-context-composer 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
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.