@backium/use-instance
v0.0.1
Published
Library to get Object Instances via context. It can be useful when you want to access your object instances from other React components.
Downloads
4
Readme
use-instance
Library to get Object Instances via context. It can be useful when you want to access your object instances from other React components.
Installation
In order to install the package. use the following command:
yarn add @backium/use-instance
Usage
First, you should add InstanceProvider
.
import { InstanceProvider } from '@backium/use-instance';
import App from './App';
export default () => {
return (
<InstanceProvider>
<App />
</InstanceProvider>
);
};
Add object instance to the store
import { InstanceProvider, useInstance } from '@backium/use-instance';
import SomeComponent from './SomeComponent';
class SecretManager {
get() {
return 'supersecret';
}
}
const App = () => {
const { setInstance } = useInstance();
const secretManager = new SecretManager();
React.useEffect(() => {
setInstance('secretManager', secretManager);
}, []);
return <SomeComponent />;
};
Use object instance from another component
import { useInstance } from '@backium/use-instance';
const SomeComponent = () => {
const [secret, setSecret] = React.useState('');
const { instances, getInstance } = useInstance();
React.useEffect(() => {
const instance = getInstance<SecretManager>('secretManager');
const secretText = instance?.get();
if (secretText) {
setSecret(secretText);
}
}, [instances]);
return <div>{secret}</div>;
};
export default SomeComponent;