Skip to main content

useInjection

The useInjection(token) hook allows you to get a dependency from a container provided through ContainerProvider.

Arguments#

  1. token: TokenValue.

Returns#

TokenType<TokenValue> โ€” a dependency bound to the token.

Example#

import { useInjection } from 'brandi-react';
import { FunctionComponent } from 'react';
import { TOKENS } from '../tokens';
export const UserComponent: FunctionComponent = () => {
const userService = useInjection(TOKENS.userService);
const logger = useInjection(TOKENS.logger.optional);
/* ... */
return (/* ... */);
}

The binding of TOKENS.userService was shown in the example in ContainerProvider section.

For more information about TOKENS.logger.optional syntax, see the Optional Dependencies documentation section.