Skip to content

Instantly share code, notes, and snippets.

@giioohbernini
Created July 28, 2020 15:20
Show Gist options
  • Save giioohbernini/b8afe3f7617a3eec18212fea19ec554e to your computer and use it in GitHub Desktop.
Save giioohbernini/b8afe3f7617a3eec18212fea19ec554e to your computer and use it in GitHub Desktop.
Web to native #4
import React, {useEffect} from 'react'
import {Link, useLocation} from 'react-router-dom'
import postMessage from './utils/postMessage.js'
const Root = () => {
const location = useLocation();
useEffect(() => {
const data = {
eventName: 'routeChange',
router: location,
location: window.location,
};
postMessage(data);
}, [location]);
/*
Logo abaixo faço uma regra para adicionar uma classe dependendo do pathname.
Irei abordar mais abaixo no artigo o motivo mas é somente um exemplo.
*/
const classNameByPath = location.pathname === '/' ? 'home' : 'about'
return (
<div className={classNameByPath}>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
</ul>
<hr />
</div>
)
}
export default Root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment