useInjection
The useInjection(token)
hook allows you to get a dependency
from a container provided through ContainerProvider
.
Arguments
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.