BlueBase
  • πŸ’§Introduction
  • Tutorial
    • 1. Getting Started
      • 1.1 Setup
      • 1.2 Add Plugins
      • 1.3 Create Custom Plugin
    • 2. Backend API
      • 2.1 Create Backend API
      • 2.2 Setup Apollo Client
      • 2.3 Generate Typescript Interfaces
    • 3. Create Screens
      • 3.1 Pending Tasks Screen
      • 3.2 Edit Task Screen
      • 3.3 Task Create Screen
      • 3.4 Tab Navigation
    • 4. CRUD Operations
      • 4.1 Creating Tasks
      • 4.2 Reading Tasks
      • 4.3 Updating Tasks
      • 4.4 Deleting Tasks
    • 5. Enhancements
      • 5.1 Internationalisation
      • 5.2 Theming
      • 5.3 Dynamic Images
      • 5.4 Settings & Configurations
      • User Management
  • Key Concepts
    • 🎑Lifecycle Events
    • ⛩️Main App Layout
    • πŸ”ŒPlugins
      • Plugin API
      • Register a Plugin
      • Making a Plugin Configurable
      • Developing an Analytics Plugin
      • Developing a Logger Plugin
      • Developing a Theme Plugin
    • πŸš‡Filters
    • 🎁Components
      • Components API
      • Registering a Component
      • Accessing Components
      • Higher Order Components
    • 🎨Themes
      • Consuming Selected Theme
      • Customise Themes
      • Customise Components
      • Theme Configs
      • Theme Structure
    • πŸŽ›οΈConfigs
  • API
    • πŸ“ˆAnalytics
    • πŸ“”Logger
    • πŸ“¦BlueBase Modules
    • Registry
  • Guides
    • βœ‚οΈCode Splitting
    • πŸ‘½Migrating to V8
  • Components
    • ActivityIndicator
    • BlueBase
      • BlueBaseApp πŸ“Œ
      • BlueBaseConsumer πŸ“Œ
      • BlueBaseFilter πŸ“Œ
      • ThemeConsumer πŸ“Œ
    • Button
    • ComponentState πŸ“Œ
    • EmptyState πŸ“Œ
    • ErrorState πŸ“Œ
    • Icons
      • Icon
      • DynamicIcon πŸ“Œ
      • PluginIcon πŸ“Œ
    • JsonSchema πŸ“Œ
    • LoadingState πŸ“Œ
    • Noop πŸ“Œ
    • Observers
      • DataObserver πŸ“Œ
      • ErrorObserver πŸ“Œ
      • HoverObserver πŸ“Œ
      • WaitObserver πŸ“Œ
    • StatefulComponent πŸ“Œ
    • Typography
    • View
Powered by GitBook
On this page
  • Through resolve method
  • Through getComponent function
  • Fallback Components
  • Accessing Raw Components

Was this helpful?

Export as PDF
  1. Key Concepts
  2. Components

Accessing Components

PreviousRegistering a ComponentNextHigher Order Components

Last updated 3 years ago

Was this helpful?

Through resolve method

In order to access a BlueBase component, just use the resolve method of Component Registry.

const Logo = BB.Components.resolve('Logo');

This will return a component wrapped in all the HOCs, and will receive a styles prop that has all the .

But in order to get access this component in your code, you will need to use a BlueBaseConsumer component to access BB context first. This is how the final code may look like:

import { BlueBase, BlueBaseConsumer } from '@bluebase/core';

const Header = () => (
    <BlueBaseConsumer>
    {(BB: BlueBase) => {
        const Logo = BB.Components.resolve('Logo');
        return <Logo />;
    }}
    </BlueBaseConsumer>
);

This adds unnecessarily long and repetitive code. Not to mention the overall ugliness and unreadability. To solve this problem, we export a helper function called getComponent .

Through getComponent function

The getComponent function returns a component from BlueBase context. By using this function we can rewrite the example above as:

import { getComponent } from '@bluebase/core';

const Logo = getComponent('Logo');

const Header = () => (<Logo />);

See how this makes the code much more developer friendly.

It is also possible to get a typed component by providing it props interface:

import { getComponent } from '@bluebase/core';

interface LogoProps {
    // ... props
}

const Logo = getComponent<LogoProps>('Logo');

To make it even more convenient, we export some basic components out of box. This helps developers not only write even less code, but also port react-native apps to BlueBase by just changing the import statement.

- import { Image, Text, View } from 'react-native';
+ import { Image, Text, View } from '@bluebase/components';

Fallback Components

Sometimes, you may need to have fallback components. So if they desired component is not found, a back up component can be used. In BlueBase it is very easy to achieve this. Just provide multiple arguments to the resolve function.

const Logo = BB.Components.resolve('AnimatedLogo', 'SingleColorLogo', 'Logo');

The first param acts as the key of the desired, with the following ones acting at fallback components in sequence.

So in the example above, first BlueBase will try to find AnimatedLogo . If it doesn't exist, BlueBase will look for SingleColorLogo . If even that isn't found, it will try to get Logo component.

This works with the getComponent function. So the code above can be written as:

const Logo = getComponent('AnimatedLogo', 'SingleColorLogo', 'Logo');

Accessing Raw Components

As explained in the above, the resolve method returns a component wrapped in the registered HOCs, and styles. But sometimes, you may need to access the raw component that was originally registered.

For that reason, BlueRain provides a getValue method that lets you access the unwrapped β€œraw” component.

const rawLogoComponent = await BB.Components.getValue('Logo');

Note that, we used the await keyword. This is because the getValue method will return the component wrapped in a promise. This is because some components may in different bundles (because of code splitting).

🎁
themed styles