TrackedContentContext
HOC wrapping the given Component
in a ContentContext and enriching it with the objectiv
prop.
TrackedContentContext: <T>(props: T & {
objectiv: {
Component: ElementType | keyof ReactHTML,
id: string,
normalizeId?: boolean
}
}) => JSX.Element
objectiv
prop attributes
type | default value | ||
---|---|---|---|
required | Component | ElementType | keyof ReactHTML | |
required | id | string | |
optional | normalizeId | boolean | true |
Returns
JSX.Element
Automatic Events
None.
Usage example
Elements
import { TrackedContentContext } from '@objectiv/tracker-react';
<TrackedContentContext<ComponentProps<'div'>>
objectiv={{
Component: 'div',
id: 'content'
}}
>
...
<TrackedContentContext<ComponentProps<'p'>>
objectiv={{
Component: 'p',
id: 'intro'
}}
>
...
</TrackedContentContext>
</TrackedContentContext>
ID normalization
By default, all Tracked Context Components automatically normalize their Context identifiers to a kebab-cased format.
This can be disabled via the normalizeId
option. This is useful when identifiers need to be preserved for cross-platform
concerns or if they are standardized identifiers, e.g. a code from a backend API.
In this example the identifier, which would normally be body-section
, will be preserved as Body Section
:
import { TrackedContentContext } from '@objectiv/tracker-react';
<TrackedContentContext<ComponentProps<'section'>>
objectiv={{
Component: 'section',
id: 'Body Section',
normalizeId: false
}}
>
...
</TrackedContentContext>
Components
The Component
prop doesn't need to be a JSX Element. Actually, more often than not, it will probably be another Component.
Here is an example of how to wrap around a hypothetical Hero component.
import { Hero, HeroProps } from '../components';
import React, { ComponentProps } from 'react';
import { TrackedContentContext } from '@objectiv/tracker-react';
export type TrackedHeroProps = HeroProps & {
objectiv?: Omit<TrackedContextObjectivProp, 'Component'>;
};
export const TrackedHero = React.forwardRef<HTMLDivElement, TrackedHeroProps>(
({ objectiv, ...nativeProps }, ref) => (
<TrackedContentContext<ComponentProps<typeof Hero>>
{...nativeProps}
ref={ref}
objectiv={{
...objectiv,
Component: Hero,
id: objectiv?.id ?? 'hero'
}}
/>
)
);
This can now be used like this:
<TrackedHero>
...
</TrackedHero>
<TrackedHero objectiv={{ id: 'hero-id' }}>
...
</TrackedHero>
<TrackedHero id={'hero-id'}>
...
</TrackedHero>
Check these how-to guides for more thorough examples:
TrackedContentContext
internally uses ContentContextWrapper.