react-native-snap-carousel icon indicating copy to clipboard operation
react-native-snap-carousel copied to clipboard

Basic typescript example need

Open Sub-Zero-1 opened this issue 5 years ago • 9 comments

Is there any basic typescript example available? I really having troubles to get in to run.

Sub-Zero-1 avatar Jan 21 '20 12:01 Sub-Zero-1

@erdalugur - could you try again to format that message?

OliverEvans96 avatar Jun 08 '20 18:06 OliverEvans96

@OliverEvans96 thanks for feedback

erdalugur avatar Jun 08 '20 18:06 erdalugur

I got a typescript example to run by merging @erdalugur 's example with the basic js example from the repository. Here it is:

import React, { Component } from "react";
import { Text, View, SafeAreaView } from "react-native";

import Carousel from "react-native-snap-carousel";

interface ItemProps {
  title: string;
  text: string;
}

// interface Props {
//   carouselItems?: ItemProps;
// }

interface State {
  activeIndex: number;
  carouselItems: ItemProps[];
}

class CustomCarousel extends Component<any, State> {
  ref = React.createRef<any>();
  state = {
    activeIndex: 0,
    carouselItems: [
      {
        title: "Item 1",
        text: "Text 1",
      },
      {
        title: "Item 2",
        text: "Text 2",
      },
      {
        title: "Item 3",
        text: "Text 3",
      },
      {
        title: "Item 4",
        text: "Text 4",
      },
      {
        title: "Item 5",
        text: "Text 5",
      },
    ],
  };
  renderItem = ({ item, index }: { item: ItemProps; index: number }) => {
    return (
      <View
        style={{
          backgroundColor: "floralwhite",
          borderRadius: 5,
          height: 250,
          padding: 50,
          marginLeft: 25,
          marginRight: 25,
        }}
      >
        <Text style={{ fontSize: 30 }}>{item.title}</Text>
        <Text>{item.text}</Text>
      </View>
    );
  };

  render() {
    return (
      <SafeAreaView style={{ flex: 1, backgroundColor: "rebeccapurple", paddingTop: 50 }}>
        <View style={{ flex: 1, flexDirection: "row", justifyContent: "center" }}>
          <Carousel
            layout={"default"}
            ref={this.ref}
            data={this.state.carouselItems}
            sliderWidth={300}
            itemWidth={300}
            renderItem={this.renderItem}
            onSnapToItem={(index: number) => this.setState({ activeIndex: index })}
          />
        </View>
      </SafeAreaView>
    );
  }
}

export default CustomCarousel;

I left the props interface there as it could be used as reference for passing down props to your component. You just need to change any in the class declaration statement for your props interface

SirKadogan avatar Jun 22 '20 00:06 SirKadogan

I have also added a functional component example in the issue mentioned above, for those who are interested.

SirKadogan avatar Jun 22 '20 01:06 SirKadogan

@SirKadogan creating your ref with type any defeats the purpose. Can we get an example where you use the real type of the Carousel?

grafficmedia avatar May 19 '21 18:05 grafficmedia

@grafficmedia There is a @types/react-native-snap-carousel library which has good types (not perfect tho) if you are still looking for it.

jihoon416 avatar Jul 18 '21 14:07 jihoon416

Sorry, please allow me to advertise for my open source library! ~ I think this library react-native-reanimated-carousel will solve your problem. It is a high performance and very simple component, complete with React-Native reanimated 2

dohooo avatar Oct 08 '21 05:10 dohooo

I have also added a functional component example in the issue mentioned above, for those who are interested.

DId you get any?

Rahmah001 avatar Jan 25 '24 08:01 Rahmah001

Please let Documentation include both CLass and functional Component structures.

You can also add Typescript too.

I couldn't use this, as typescript complained

Rahmah001 avatar Jan 25 '24 08:01 Rahmah001