-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example on how to expose the instance globally (#413)
- Loading branch information
1 parent
1638ffd
commit fc79b80
Showing
10 changed files
with
862 additions
and
843 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {render} from '@testing-library/react'; | ||
import croct, {Plug} from '@croct/plug'; | ||
import {GlobalCroct} from '@/components/GlobalCroct'; | ||
|
||
declare global { | ||
interface Window { | ||
croct?: Plug; | ||
} | ||
} | ||
|
||
describe('<Providers />', () => { | ||
it('should expose the Croct instance on the global scope', () => { | ||
expect(window.croct).toBeUndefined(); | ||
|
||
render(<GlobalCroct />); | ||
|
||
expect(window.croct).toBe(croct); | ||
}); | ||
}); |
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,31 @@ | ||
'use client'; | ||
|
||
import {FunctionComponent, useEffect} from 'react'; | ||
import croct from '@croct/plug'; | ||
|
||
/* | ||
* NOTE | ||
* | ||
* This component makes the `croct` instance available on the `window` | ||
* object so tools like Google Tag Manager can use it. | ||
* | ||
* Although functional, we do not recommend this approach as it can | ||
* lead to problems like race conditions where Google Tag Manager may | ||
* run before the `useEffect` hook. | ||
* | ||
* For more reliable and efficient event tracking, use the `useCroct` | ||
* hook directly in your application. | ||
*/ | ||
|
||
export const GlobalCroct: FunctionComponent = () => { | ||
useEffect( | ||
() => { | ||
if (!('croct' in window)) { | ||
Object.assign(window, {croct: croct}); | ||
} | ||
}, | ||
[], | ||
); | ||
|
||
return null; | ||
}; |
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
Oops, something went wrong.