๐ Custom Provider and Hook for managing config in React Apps.
- Use Case
- Compatibility
- Installation
- Example Usage
- Provider API
- useConfig API
- Issues
- Contributing
- License
You need a proper way to access config from your react components and custom hooks.
Our package currently supports apps with React >= 16.8.0. You can use this package for React and React Native Apps.
You can install this library via NPM or YARN.
npm i @blackbox-vision/react-use-config
yarn add @blackbox-vision/react-use-config
After reading and performing the previous steps, you should be able to import the library and use it like in this example:
- Define the
config
and wrap your App inConfigProvider
:
import React from 'react';
import ReactDOM from 'react-dom';
import { ConfigProvider } from '@blackbox-vision/react-use-config';
import Header from './Header';
const config = {
logo: 'https://xyz.com/logo.png',
};
const App = () => (
<ConfigProvider config={config} debug={false}>
<Header />
</ConfigProvider>
);
export default App;
- Import the
hook
and consume the config part you really need:
import React from 'react';
import { useConfig } from '@blackbox-vision/react-use-config';
const Header = (props) => {
const logoUri = useConfig(config => config?.logo);
return (
<div>
<img src={logoUri} />
{...}
</div>
);
};
export default Header;
The ConfigProvider
component has the following props:
Properties | Types | Default Value | Description |
---|---|---|---|
config | object | none | Property that represents the config object |
getConfig | function | none | Property that a function to load config async |
debug | boolean | false | Property that enables debug mode |
The useConfig
hook lets you retrieve the full config object or a specific config part using a selector.
The hook receives the following parameters:
selector
: It's an optional function that gives you the config and let you return an specific value that will be memoized.
If you pass the selector
to the useConfig
hook you'll receive an specific config item. In case you didn't, you'll only receive the fully config object.
Please, open an issue following one of the issues templates. We will do our best to fix them.
If you want to contribute to this project see contributing for more information.
Distributed under the MIT license. See LICENSE for more information.