-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from CarsonF/feature/hooks
Including Hooks support 🎉
- Loading branch information
Showing
5 changed files
with
91 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { default as Split } from './Split'; | ||
export { default as SplitProvider, SplitContext } from './SplitProvider'; | ||
export * from './useSplit'; | ||
export * from './useTrack'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
Attributes, | ||
TreatmentWithConfig, | ||
} from '@splitsoftware/splitio/types/splitio'; | ||
import { useContext, useEffect, useState } from 'react'; | ||
import { SplitContext } from './SplitProvider'; | ||
|
||
/** | ||
* Returns a treatment and it's config. | ||
* @param {string} splitName - The string that represents the split we want to get the treatment. | ||
* @param {Attributes=} attributes - An object of type Attributes defining the attributes for the given key. | ||
* @returns {[string, string | null]} Tuple with treatment first and config second. | ||
*/ | ||
export const useSplit = ( | ||
splitName: string, | ||
attributes?: Attributes, | ||
): [string, string | null] => { | ||
const { client, isReady, lastUpdate } = useContext(SplitContext); | ||
const [{ treatment, config }, setTreatment] = useState(defaultTreatment); | ||
useEffect(() => { | ||
const next = | ||
client && isReady | ||
? client.getTreatmentWithConfig(splitName, attributes) | ||
: defaultTreatment; | ||
setTreatment(next); | ||
}, [client, isReady, lastUpdate]); | ||
return [treatment, config]; | ||
}; | ||
|
||
const defaultTreatment: TreatmentWithConfig = { | ||
config: null, | ||
treatment: 'control', // SplitIO's default value | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IClient } from '@splitsoftware/splitio/types/splitio'; | ||
import { useContext } from 'react'; | ||
import { SplitContext } from './SplitProvider'; | ||
|
||
export const useTrack: () => IClient['track'] = () => { | ||
const { client, isReady } = useContext(SplitContext); | ||
return client && isReady ? client.track : defaultTrack; | ||
}; | ||
|
||
const defaultTrack = () => false; |