-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): let router maintain state and scroll to top
- Loading branch information
Gerd Müller
committed
Jun 27, 2020
1 parent
36537c1
commit 3eb6a37
Showing
5 changed files
with
57 additions
and
13 deletions.
There are no files selected for viewing
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,23 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { withRouter } from 'react-router-dom'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const ScrollIntoView = ({ children, location }) => { | ||
const prevLocation = useRef(); | ||
|
||
useEffect(() => { | ||
if (prevLocation.current !== location.pathname) { | ||
document.querySelector('.parallax__header').scrollIntoView({ behavior: 'smooth' }); | ||
prevLocation.current = location.pathname; | ||
} | ||
}, [location]); | ||
|
||
return children; | ||
}; | ||
|
||
ScrollIntoView.propTypes = { | ||
children: PropTypes.node, | ||
location: PropTypes.object | ||
}; | ||
|
||
export default withRouter(ScrollIntoView); |
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,12 @@ | ||
import React from 'react'; | ||
|
||
export default function usePersistedState(key, defaultValue) { | ||
const [state, setState] = React.useState(() => { | ||
const persistedState = localStorage.getItem(key); | ||
return persistedState ? JSON.parse(persistedState) : defaultValue; | ||
}); | ||
React.useEffect(() => { | ||
window.localStorage.setItem(key, JSON.stringify(state)); | ||
}, [state, key]); | ||
return [state, setState]; | ||
} |
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 |
---|---|---|
@@ -1,16 +1,21 @@ | ||
import React from 'react'; | ||
|
||
import React, {useState} from 'react'; | ||
import { Redirect } from "react-router-dom"; | ||
import './redirect.scss'; | ||
|
||
/* eslint-disable-next-line */ | ||
export interface RedirectProps { | ||
value: string | ||
layer: string | ||
} | ||
|
||
export const Redirect = (props: RedirectProps) => { | ||
export const RedirectComponent = (props: RedirectProps) => { | ||
const [redirect, setRedirect] = useState(false); | ||
return ( | ||
<button type='button' className='link-button redirect'>{props.value}</button> | ||
<> | ||
<button type='button' className='link-button redirect' onClick={() => setRedirect(true)}>{props.value}</button> | ||
{redirect && <Redirect exact to={props.layer} />} | ||
</> | ||
); | ||
}; | ||
|
||
export default Redirect; | ||
export default RedirectComponent; |