2.0.7 • Published 2 years ago

react-context-utilities v2.0.7

Weekly downloads
18
License
MIT
Repository
github
Last release
2 years ago

react-context-utilities

Utilities to productively create and combine new ReactJS context.

Installation

yarn add react-context-utilities

Usage

You can use createSafeContext to create a safe ReactJS context instance. Whenever you try to render the Context.Consumer without rendering Context.Provider above it, it'll throw an error.

interface IConfig {
    maxWaitTime: number;
}
/**
 * createSafeContext will make sure it won't need to provide
 * a default value for it since it'll throw an error
 * if you try to consume without a provider above it
 */
const ConfigContext = createSafeContext<IConfig>({
    name: 'ConfigContext'
});
function mapContextToProps({ config }: { config: ConfigContext; }) {
    return {
        config
    };
}
const Menu = ({ config }: { config: IConfig; }) => {
    return <React.Fragment>
        Max response time is {config.maxWaitTime}
    </React.Fragment>;
};
/**
 * As expected, `MenuWithContext` will no longer ask
 * for `config` property in type annotation
 */
const MenuWithContext = withContext({
    config: ConfigContext
}, mapContextToProps)(Menu);
const wrapper = shallow(<ConfigContext.Provider value={{ maxWaitTime: 1000 }}>
    <MenuWithContext/>
</ConfigContext.Provider>);
2.0.5

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago