next Router push code example
Example 1: next router push state
Router.push({
pathname: '/about',
query: { name: 'Someone' }
})
Example 2: router nextjs
import { useRouter } from 'next/router'
function ActiveLink({ children, href }) {
const router = useRouter()
const handleClick = (e) => {
e.preventDefault()
router.push(href)
}
return (
<a href={href} onClick={handleClick} style={style}>
{children}
</a>
)
}
export default ActiveLink
Example 3: nextjs router get complete url
function getFullUrl(req, fallback) {
if(req) {
return req.protocol + '://' + req.get('host') + req.originalUrl
}
else if(!(typeof window === 'undefined')) {
return window.location.href
} else {
return fallback
}
}
static async getInitialProps({req}) {
let fullUrl = getFullUrl(req, "")
return { fullUrl: fullUrl }
}
Example 4: next router push state
import { withRouter } from 'next/router'
class About extends React.Component {
}
export default withRouter(About)