Work with Ninetailed Experiences, instances, and profiles within React and React-based meta-frameworks.
<NinetailedProvider>
The <NinetailedProvider> provides a Ninetailed instance it to the application. It can accept all of the same instantiation options as a Ninetailed instance to create a new Ninetailed instance or accept an existing Ninetailed instance.
importtype { Ninetailed } from"@ninetailed/experience.js"import { NinetailedProvider } from"@ninetailed/experience.js-react"// or import { NinetailedProvider } from '@ninetailed/experience.js-next' // or import { NinetailedProvider } from '@ninetailed/experience.js-gatsby'importtype { NinetailedProviderInstantiationProps } from"@ninetailed/experience.js-react"// Most implementations will rely on the provider to create the Ninetailed instance by passing instantation propsfunction createWithInstanceProps({createInstanceProps, children}: {createInstanceProps: NinetailedProviderInstantiationProps, children: ReactNode }) {
return ( <NinetailedProvider {...createInstanceProps}> {children} </NinetailedProvider> )}// However, you can also pass an existing Ninetailed instance to the `ninetailed` prop// Use this if you need to configure fetchImpl on the NinetailedAPIClientfunctioncreateWithNinetailedInstance({ninetailed, children}: {ninetailed:Ninetailed, children:ReactNode }) {return ( <NinetailedProviderninetailed={ninetailed}> {children} </NinetailedProvider> )}
The provided Ninetailed instance can be retreived with the useNinetailed hook.
Returns the loading state of an experience and once loaded, the all relevant metadata of the experience including the assigned variant and variant index. This hook powers the React <Experience> component exported by the SDKs.
This hook is useful when you need imperative access to the metadata an assigned experience, often in circumstances where you are not intending to render out CMS-sourced content variations. For example, you might be using a feature-flag style experiment where you have no need for the <Experience> component, but your application still needs to change behavior and/or send tracking calls based on the assigned variantIndex.
Retrieves the Ninetailed instance from the nearest <NinetailedProvider> and all of its instance methods and properties as in the Ninetailed Instance documentation.
import { useNinetailed } from'@ninetailed/experience.js-react';// or import { useNinetailed } from '@ninetailed/experience.js-next' // or import { useNinetailed } from '@ninetailed/experience.js-gatsby'constYourComponent= () => {const { debug,identify,observeElement,onIsInitialized,onProfileChange,page,plugins,profileState,reset,track,trackComponentView,unobserveElement } =useNinetailed();// Go wild}
useProfile
Retrieves the current profile state from the nearest <NinetailedProvider>.
import { useProfile } from'@ninetailed/experience.js-react';// or import { useProfile } from '@ninetailed/experience.js-next' // or import { useProfile } from '@ninetailed/experience.js-gatsby'constGreetingComponent= () => {const { error,from,loading,profile,status }: { error:Error|null, from:'api'|'hydrated', loading:boolean, profile:Profile|null, status:"error"|"loading"|"success" } =useProfile();return <p>Hey {profile.traits.firstname}, nice weather in {profile.location.city}!</p>}