react-native-card-stack-swiper icon indicating copy to clipboard operation
react-native-card-stack-swiper copied to clipboard

TypeError: undefined is not an object ('_this.swiper=swiper')

Open Marvin-Bai opened this issue 5 years ago • 10 comments

It seems there is something wrong in here: ref={ swiper => { this.swiper = swiper }}

Marvin-Bai avatar Mar 24 '20 04:03 Marvin-Bai

What is wrong exactly?

dancomanlive avatar Apr 29 '20 19:04 dancomanlive

I am getting the same error when I try to incorporate this project into my existing code or even just run the demo itself. I don't know enough about JavaScript to know what's wrong, just that I always get the "TypeError: undefined is not an object ('_this.swiper=swiper')" error each time, likely at that line cited in the comment above,

<CardStack style={styles.content} ref={swiper => { this.swiper = swiper }}>
      <Card style={[styles.card, styles.card1]}><Text style={styles.label}>A</Text></Card>
      <Card style={[styles.card, styles.card2]}><Text style={styles.label}>B</Text></Card>
      <Card style={[styles.card, styles.card1]}><Text style={styles.label}>C</Text></Card>
</CardStack> 

What exactly is going with that ref? And why don't I see "ref" anywhere in the CardStack source code?

ryanfrisch avatar Jun 29 '20 00:06 ryanfrisch

Ya I'm seeing the same thing.

mhkeller avatar Aug 26 '20 00:08 mhkeller

I am seeing the very same error, Have any one of you figured it out ?

abdelrhman92 avatar Sep 10 '20 18:09 abdelrhman92

I ended up not using this library but it seems like it’s just trying to keep track of variable references. if you make an empty object and then assign the swiper variable to that it seems to work. I'm not really sure how or why this is helpful though so it may be fine just to remove.

const t = {};
// and then

<CardStack style={styles.content} ref={swiper => { t.swiper = swiper }}>

EDIT: I see it's used in instances like this below where you want to use methods on the swiper. Seems like this may also be a fix: https://github.com/lhandel/react-native-card-stack-swiper/issues/73

<CardStack style={styles.content} ref={swiper => { this.swiper = swiper }}>
    <Card style={[styles.card, styles.card1]}><Text style={styles.label}>A</Text></Card>
    <Card style={[styles.card, styles.card2]}><Text style={styles.label}>B</Text></Card>
  </CardStack>

  <TouchableOpacity onPress={ () => { this.swiper.swipeLeft() }}>
    <Text>Left</Text>
  </TouchableOpacity>

mhkeller avatar Sep 10 '20 19:09 mhkeller

Do you use that in a functional component or a class component?

thibaut-d avatar Nov 28 '20 20:11 thibaut-d

For a functional component the proper way to use it may be more:

const [swiper, setSwiper] = useState<CardStack | null>(null)

  return (
    <CardStack
      style={styles.container}
      ref={(newSwiper): void => {
        setSwiper(newSwiper)
      }}>
      <Card style={[styles.card, styles.card1]}>
        <Text style={styles.label}>A</Text>
      </Card>
      <Card style={[styles.card, styles.card2]}>
        <Text style={styles.label}>B</Text>
      </Card>
      <Card style={[styles.card, styles.card1]}>
        <Text style={styles.label}>C</Text>
      </Card>
    </CardStack>
  )
}

thibaut-d avatar Nov 28 '20 20:11 thibaut-d

For a functional component the proper way to use it may be more:

const [swiper, setSwiper] = useState<CardStack | null>(null)

  return (
    <CardStack
      style={styles.container}
      ref={(newSwiper): void => {
        setSwiper(newSwiper)
      }}>
      <Card style={[styles.card, styles.card1]}>
        <Text style={styles.label}>A</Text>
      </Card>
      <Card style={[styles.card, styles.card2]}>
        <Text style={styles.label}>B</Text>
      </Card>
      <Card style={[styles.card, styles.card1]}>
        <Text style={styles.label}>C</Text>
      </Card>
    </CardStack>
  )
}

You should add this to the docs then.

robertmylne avatar Feb 08 '21 03:02 robertmylne

For a functional component the proper way to use it may be more:

const [swiper, setSwiper] = useState<CardStack | null>(null)

  return (
    <CardStack
      style={styles.container}
      ref={(newSwiper): void => {
        setSwiper(newSwiper)
      }}>
      <Card style={[styles.card, styles.card1]}>
        <Text style={styles.label}>A</Text>
      </Card>
      <Card style={[styles.card, styles.card2]}>
        <Text style={styles.label}>B</Text>
      </Card>
      <Card style={[styles.card, styles.card1]}>
        <Text style={styles.label}>C</Text>
      </Card>
    </CardStack>
  )
}

You should add this to the docs then.

@thibaut-d c Even though state gets updated with the ref of CardStack, if you try invoking any of the swipeRight or swipeLeft actions, I'm not seeing it do anything.

owenashurst avatar Jun 18 '21 21:06 owenashurst

I ended up not using this library but it seems like it’s just trying to keep track of variable references. if you make an empty object and then assign the swiper variable to that it seems to work. I'm not really sure how or why this is helpful though so it may be fine just to remove.

const t = {};
// and then

<CardStack style={styles.content} ref={swiper => { t.swiper = swiper }}>

EDIT: I see it's used in instances like this below where you want to use methods on the swiper. Seems like this may also be a fix: #73

<CardStack style={styles.content} ref={swiper => { this.swiper = swiper }}>
    <Card style={[styles.card, styles.card1]}><Text style={styles.label}>A</Text></Card>
    <Card style={[styles.card, styles.card2]}><Text style={styles.label}>B</Text></Card>
  </CardStack>

  <TouchableOpacity onPress={ () => { this.swiper.swipeLeft() }}>
    <Text>Left</Text>
  </TouchableOpacity>

Bro, you've have just saved my life. 👍

aliraza96 avatar Nov 26 '21 05:11 aliraza96