React-native-keyboard-aware-scroll-view: scrollToFocusedInput in functional component

Created on 14 Oct 2019  路  5Comments  路  Source: APSL/react-native-keyboard-aware-scroll-view

Does anyone have an example of how to implement the scrollToFocusedInput in a functional component with hooks (no classes). Thanks

Most helpful comment

I am using react hooks and I am assigning ref like this.

let listRef = useRef();
const _scrollToTop = () => {
listRef.current.scrollToIndex({animated: true, index: 0})
}


<KeyboardAwareFlatList
          innerRef={r => (listRef = r)}
/>


I have also tried
listRef.props.scrollToPosition(0, 0)

Both of these don't work and listRef.current throws undefined error.

All 5 comments

@jjg77 I want to scroll to top, but not working for me. Have you had any luck?

Yes, I did something like this. You place this function with your flatlist and pass it as a prop to the items.

const _scrollToIndex = index => {
    //console.log('called _scrollToIndex: ', index);
    if (index <= comments.length && index >= 0){
      flatListRef.current.scrollToIndex({animated: true, index: index})
    }else{
      console.log('index out of range, scroll to bottom');
      flatListRef.current.scrollToIndex({animated: true, index: comments.length-1})
    }
  }

I am using react hooks and I am assigning ref like this.

let listRef = useRef();
const _scrollToTop = () => {
listRef.current.scrollToIndex({animated: true, index: 0})
}


<KeyboardAwareFlatList
          innerRef={r => (listRef = r)}
/>


I have also tried
listRef.props.scrollToPosition(0, 0)

Both of these don't work and listRef.current throws undefined error.

@jjg77 how are assigning the ref? I'm getting null or undefined ref value.

const _scrollToTop = () => {
listRef?.current?.scrollToPosition(0,0)
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clfristoe picture clfristoe  路  4Comments

diegorodriguesvieira picture diegorodriguesvieira  路  4Comments

yasaricli picture yasaricli  路  3Comments

brunolemos picture brunolemos  路  3Comments

pinkladywithcoco picture pinkladywithcoco  路  3Comments