summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authorHaishan <[email protected]>2019-01-13 23:15:54 +0800
committerHaishan <[email protected]>2019-01-13 23:15:54 +0800
commitfa7e03b9265ea18278ed785a2f48231722dd28d8 (patch)
tree2f5519b10b557b388182ec4119e2ae874f2ecc19 /src/components
parent61dd81d488662ce43af0193796673ebf01dd0d90 (diff)
hooks: extract element remaining view port height logic into a hook
Diffstat (limited to 'src/components')
-rw-r--r--src/components/Rules.js19
1 files changed, 3 insertions, 16 deletions
diff --git a/src/components/Rules.js b/src/components/Rules.js
index b282919..8e3f991 100644
--- a/src/components/Rules.js
+++ b/src/components/Rules.js
@@ -13,6 +13,7 @@ import { FixedSizeList as List, areEqual } from 'react-window';
import ContentHeader from 'c/ContentHeader';
import Rule from 'c/Rule';
import RuleSearch from 'c/RuleSearch';
+import useRemainingViewPortHeight from '../hooks/useRemainingViewPortHeight';
import { getRules, fetchRules, fetchRulesOnce } from 'd/rules';
@@ -45,24 +46,10 @@ const Row = memo(({ index, style, data }) => {
export default function Rules() {
const { fetchRulesOnce, fetchRules } = useActions(actions);
const { rules } = useStoreState(mapStateToProps);
- const refRulesContainer = useRef(null);
- const [containerHeight, setContainerHeight] = useState(200);
- function _updateContainerHeight() {
- const { top } = refRulesContainer.current.getBoundingClientRect();
- setContainerHeight(window.innerHeight - top - paddingBottom);
- }
- const updateContainerHeight = useCallback(_updateContainerHeight, []);
-
useEffect(() => {
fetchRulesOnce();
}, []);
- useLayoutEffect(() => {
- updateContainerHeight();
- window.addEventListener('resize', updateContainerHeight);
- return () => {
- window.removeEventListener('resize', updateContainerHeight);
- };
- }, []);
+ const [refRulesContainer, containerHeight] = useRemainingViewPortHeight();
return (
<div>
@@ -70,7 +57,7 @@ export default function Rules() {
<RuleSearch />
<div ref={refRulesContainer} style={{ paddingBottom }}>
<List
- height={containerHeight}
+ height={containerHeight - paddingBottom}
width="100%"
itemCount={rules.length}
itemSize={80}