react-native-keyboard-aware-scroll-view icon indicating copy to clipboard operation
react-native-keyboard-aware-scroll-view copied to clipboard

Support React.createRef()

Open slorber opened this issue 6 years ago • 21 comments

Currently passing a ref to innerRef does not work, as we have to provide a function

A workaround is simple:

  constructor(props) {
    super(props);
    this.scrollViewRef = React.createRef();
  }

  handleRef = ref => {
    this.scrollViewRef.current = ref;
  };

slorber avatar Jun 06 '18 18:06 slorber

i dont understand, should this work?

constructor(props) {
    super(props);
    this._scroll = React.createRef();
  }.
...
<KeyboardAwareScrollView
      style={styles.registrationBody}
      keyboardShouldPersistTaps="handled"
      innerRef={ref => { this._scroll.current = ref; }}
    >
....

    this._scroll.scrollToEnd();

coz for me, it says Cannot read property 'scrollToEnd' of undefined still.

khizar avatar Aug 14 '18 10:08 khizar

@khizar innerRef is the RN scrollview ref, not the KeyboardAwareScrollView ref (which has the method you want).

Use innerRef to get the component reference and use this.scrollRef.props to access these methods.

Are you using this.scrollRef.props. scrollToEnd()? don't forget the props part here.

I think the current API is a bit confusing, because the public api seems to be exposed to the raw RN scrollview as props which feels a bit unnatural. @alvaromb what about exposing a public API on the wrapper component itself?

slorber avatar Aug 14 '18 10:08 slorber

yes i tried that as well. the _scroll in my code is set to

{
  current: null
}

which i think means that the ref is not set? i am doing a console log on it before calling the function. maybe i didnt get this properly but my understanding here is that using innerRef like this should set the ref to the RN scrollview to _scroll and that scrollview is passed this function as a prop which i can use. Please correct me if i am wrong here :(

khizar avatar Aug 14 '18 11:08 khizar

@khizar maybe you can try to log the ref here:

innerRef={ref => { console.log("innerRef",ref); this._scroll.current = ref; }}

could it be that you are calling the scrollToEnd after the scrollview is unmounted?

slorber avatar Aug 14 '18 11:08 slorber

@slorber thanks for the help, i am very new to React Native and might be doing something stupid but this is not working for me. I am staying on the same screen and all the content is inside the <KeyboardAwareScrollView /> so i am sure that it is not unmounted. About the console logs, nothing gets logged if i do what you said above. But instead if i do ref={this._scroll} and the log this._scroll in the componentDidMount, i get screen shot 2018-08-14 at 15 08 56

Which i assume is a ref to the KeyboardAwareScrollView and from what i understood above, i am not to use this?? also this does not have a props.scrollToEnd func, although it does have a scrollToBottom function, which is apparently doing nothing. But i am assuming that is because i am not supposed to use this. Do i make any sense here?

khizar avatar Aug 14 '18 13:08 khizar

if it helps, i would add here what i am actually trying to achieve. So i have a form that is wrapped in this component and when its valid, i add the call to action button. So if the button is outside this scrollview, it just jumps above the keyboard on android and we can achieve the same thing using KeyboardAvoidingView on ios. But then it overlaps the last part of the form, just above the keyboard. So to avoid that, i am adding the button inside this scrollview as well, but now it stays under the keyboard and i want the scrollview to scroll up so that the button becomes visible without closing the keyboard. So my approach here is to call scrollToEnd on button mount but this is where i am stuck. So maybe you can let me know if the way i am using this component is wrong. thanks.

khizar avatar Aug 14 '18 13:08 khizar

@khizar it's really hard to help you if you don't provide code. Please go to https://snack.expo.io/ and create a simple reproduction case so that I can take a look.

Btw you can also take a look at react-native-scroll-into-view, it does not handle keyboard offset (yet) but you can probably work around that by providing a bottom inset.

slorber avatar Aug 14 '18 13:08 slorber

okay thanks, i will try and reproduce it there later and in the meantime, post back if i find a solution. thanks.

khizar avatar Aug 14 '18 14:08 khizar

What should we do here, @slorber ?

alvaromb avatar Aug 22 '18 14:08 alvaromb

@alvaromb about issue of @khizar we wait for a repro case

About original issue, i'd like to create a package which aims to help lib authors to intercept refs, supporting functions and createRef.

Related tweet: https://twitter.com/sebastienlorber/status/1013735660161822720

slorber avatar Aug 22 '18 14:08 slorber

Cool, let me know if I can help here.

alvaromb avatar Aug 22 '18 15:08 alvaromb

when i log the ref, I get a null value, so the variable this.scrollRef remains undefined

rarenatoe avatar Mar 21 '19 01:03 rarenatoe

In my functional component it works this way (other solutions mentioned above did not work for me):

const SearchScreen = (props: IProps) => {

  let scrollRef = React.createRef();

  const scrollToTop = () => {
    scrollRef.current.scrollToPosition(0, 0);
  }

  return (
      <KeyboardAwareScrollView
        ref={scrollRef}
      >
        ...
      </KeyboardAwareScrollView>
  )
}

felix4321 avatar Mar 30 '20 14:03 felix4321

In my functional component it works this way (other solutions mentioned above did not work for me):

const SearchScreen = (props: IProps) => {

  let scrollRef = React.createRef();

  const scrollToTop = () => {
    scrollRef.current.scrollToPosition(0, 0);
  }

  return (
      <KeyboardAwareScrollView
        ref={scrollRef}
      >
        ...
      </KeyboardAwareScrollView>
  )
}

We have the exact same usage :( The inner-ref concept doesn't work for us..

kg-currenxie avatar Apr 28 '20 06:04 kg-currenxie

Can't make it work with functional component as well :(

lumberman avatar May 09 '20 06:05 lumberman

hi guys, anyone has made it work?

ys-sherzad avatar Jun 03 '20 11:06 ys-sherzad

Any update on this?

KPS250 avatar Oct 05 '20 19:10 KPS250

In my functional component it works this way (other solutions mentioned above did not work for me):

const SearchScreen = (props: IProps) => {

  let scrollRef = React.createRef();

  const scrollToTop = () => {
    scrollRef.current.scrollToPosition(0, 0);
  }

  return (
      <KeyboardAwareScrollView
        ref={scrollRef}
      >
        ...
      </KeyboardAwareScrollView>
  )
}

The error below occurs to me. Type 'RefObject<unknown>' is not assignable to type '(ref: Element) => void'.

ordidxzero avatar Mar 17 '21 01:03 ordidxzero

Currently passing a ref to innerRef does not work, as we have to provide a function

@slorber It looks straightforward to implement this. Is it pending a decision?

(I hope you don't mind the notification 🙏🏼)

texastoland avatar Jan 19 '22 14:01 texastoland

@texastoland I'm not using this lib for 2 years now and don't plan to maintain it myself.

Reach out to APSL for support, I'm just a benevolent contributor and not the creator/owner of that lib.

slorber avatar Jan 21 '22 14:01 slorber

Ah thanks for the reply 👌🏼 Yeah I just found it cleaning up a contract. Not that big a deal!

texastoland avatar Jan 21 '22 14:01 texastoland