Last active
December 13, 2021 20:06
-
-
Save ghankerson/e9cd0ff47db9c89aab165a2c555ba25b to your computer and use it in GitHub Desktop.
Viafoura/React/Next JS/Single page app implementation
This file contains hidden or 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
| import Head from 'next/head'; | |
| import { useRouter } from 'next/router'; | |
| import loadScript from '../../utils/loadScript'; | |
| const Content = ({ | |
| useViafoura, | |
| }) => { | |
| const router = useRouter(); | |
| const source = '//cdn.viafoura.net/vf-v2.js'; | |
| const [viaFouraReady, setViafouraReady] = useState(false); | |
| useEffect(() => { | |
| function reset() { | |
| setViafouraReady(true); | |
| window.vfQ = window.vfQ || []; | |
| window.vfQ.push(function () { | |
| window.vf.context.reset(); | |
| }); | |
| } | |
| if (!viaFouraReady) { | |
| loadScript(viafouraId, source, reset); | |
| } else { | |
| reset(); | |
| } | |
| router.events.on('routeChangeEnd', reset); | |
| return () => { | |
| reset(); | |
| }; | |
| }, [id]); | |
| return ( | |
| <article className={classes}> | |
| <p>Normal content here.</p> | |
| <div className="content_body userContent"> | |
| {typeof useViafoura != 'undefined' && viaFouraReady && ( | |
| <> | |
| <Head> | |
| <meta name="vf:container_id" content={`vfid-${id}`} /> | |
| </Head> | |
| <div className="viafoura" style={{ margin: '1em 0 0' }}> | |
| <vf-livestory></vf-livestory> | |
| <div></div> | |
| <vf-tray-trigger floating="true"></vf-tray-trigger> | |
| </div> | |
| </> | |
| )} | |
| </div> | |
| </article> | |
| ); | |
| }; | |
| export default Content; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment