Skip to content

Commit

Permalink
Fix scroll view ref issue (#1)
Browse files Browse the repository at this point in the history
Use offset in ScrollToView
Add ESLint and Prettier as dev dependencies
Fix ESLint issues
  • Loading branch information
isokolovskii authored Mar 10, 2020
1 parent f91503a commit 4a63f36
Show file tree
Hide file tree
Showing 5 changed files with 1,425 additions and 179 deletions.
19 changes: 19 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
root: true,
extends: ['@react-native-community', 'prettier'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'import'],
rules: {
'arrow-body-style': ['error', 'as-needed'],
'no-console': 'error',
'react/jsx-key': 'error',
'react-hooks/exhaustive-deps': 'off',
'import/order': [
'error',
{
groups: ['external', 'internal', 'builtin', 'parent', 'unknown', 'index', 'sibling'],
'newlines-between': 'always',
},
],
},
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@
"react-native": ">=0.59.0-rc.0 <1.0.x"
},
"devDependencies": {
"@react-native-community/eslint-config": "^0.0.7",
"@types/react": "^16.9.19",
"@types/react-native": "^0.61.12",
"@typescript-eslint/eslint-plugin": "^2.23.0",
"@typescript-eslint/parser": "^2.23.0",
"auto-changelog": "^1.16.2",
"eslint": "^6.8.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-prettier": "^3.1.2",
"prettier": "^1.19.1",
"react": "^16.12.0",
"react-native": "^0.59.10",
"typescript": "^3.7.5"
"typescript": "^3.8.2"
}
}
19 changes: 5 additions & 14 deletions src/CollapsibleNavBarScrollView.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
import React, {
memo,
useRef,
useCallback,
useEffect,
useMemo,
ReactNode,
useState,
forwardRef,
useImperativeHandle,
} from 'react'
import React, { memo, useRef, useCallback, useEffect, useMemo, useState, forwardRef, useImperativeHandle } from 'react'
import {
Animated,
ScrollView,
Expand All @@ -17,7 +7,6 @@ import {
NativeScrollEvent,
ScrollViewProps,
LayoutChangeEvent,
findNodeHandle,
} from 'react-native'

import useTimeout from './useTimeout'
Expand Down Expand Up @@ -174,9 +163,11 @@ export const CollapsibleNavBarScrollView = memo(
)

const scrollToView = useCallback((viewRef: View, offset = 0) => {
if (viewRef && viewRef.measure && contentScrollViewRef.current) {
if (viewRef && viewRef.measure) {
viewRef.measure((ox, oy) => {
contentScrollViewRef.current.scrollTo({ y: oy })
if (contentScrollViewRef.current) {
contentScrollViewRef.current.scrollTo({ y: oy + offset })
}
})
}
}, [])
Expand Down
31 changes: 14 additions & 17 deletions src/useTimeout.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { useRef, useEffect, useCallback } from "react";
import { useRef, useEffect, useCallback } from 'react'

const useTimeout = (): [
(callback: () => void, delay: number) => void,
() => void
] => {
const timer = useRef<number | null>(null);
const useTimeout = (): [(callback: () => void, delay: number) => void, () => void] => {
const timer = useRef<number | null>(null)

const clearTimer = useCallback(() => {
if (timer.current) {
clearTimeout(timer.current);
clearTimeout(timer.current)
}
}, []);
}, [])

useEffect(() => clearTimer, []);
useEffect(() => clearTimer, [])

const setTimer = useCallback((callback: () => void, delay: number) => {
clearTimer();
clearTimer()
timer.current = setTimeout(() => {
timer.current = null;
callback();
}, delay);
}, []);
timer.current = null
callback()
}, delay)
}, [])

return [setTimer, clearTimer];
};
return [setTimer, clearTimer]
}

export default useTimeout;
export default useTimeout
Loading

0 comments on commit 4a63f36

Please sign in to comment.