Created
July 28, 2020 15:20
-
-
Save giioohbernini/b8afe3f7617a3eec18212fea19ec554e to your computer and use it in GitHub Desktop.
Web to native #4
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 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