Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination Examlpe For Javascript instead of TypeScript #755

Open
shubhamkackar0203 opened this issue Jan 5, 2025 · 1 comment
Open
Assignees
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@shubhamkackar0203
Copy link

i want to show the pagination component for my Carousel. The official document has the following code

import * as React from "react";
import { Dimensions, Text, View } from "react-native";
import { useSharedValue } from "react-native-reanimated";
import Carousel, {
ICarouselInstance,
Pagination,
} from "react-native-reanimated-carousel";

const data = [...new Array(6).keys()];
const width = Dimensions.get("window").width;

function App() {
const ref = React.useRef(null);
const progress = useSharedValue(0);

const onPressPagination = (index: number) => {
ref.current?.scrollTo({
/**
* Calculate the difference between the current index and the target index
* to ensure that the carousel scrolls to the nearest index
*/
count: index - progress.value,
animated: true,
});
};

return (
<View style={{ flex: 1 }}>
<Carousel
ref={ref}
width={width}
height={width / 2}
data={data}
onProgressChange={progress}
renderItem={({ index }) => (
<View
style={{
flex: 1,
borderWidth: 1,
justifyContent: "center",
}}
>
<Text style={{ textAlign: "center", fontSize: 30 }}>{index}

)}
/>

  <Pagination.Basic
    progress={progress}
    data={data}
    dotStyle={{ backgroundColor: "rgba(0,0,0,0.2)", borderRadius: 50 }}
    containerStyle={{ gap: 5, marginTop: 10 }}
    onPress={onPressPagination}
  />
</View>

);
}

export default App;

However, Pagination,Basic is not working for JS. Can someone help me with this

@shubhamkackar0203 shubhamkackar0203 added the documentation Improvements or additions to documentation label Jan 5, 2025
@dosubot dosubot bot added the bug Something isn't working label Jan 5, 2025
Copy link

dosubot bot commented Jan 5, 2025

Hey, @shubhamkackar0203! I'm here to help you with your issue. Let's take a look at the problem together.

The example you provided is written in TypeScript, but it can be easily adapted for JavaScript. Here's how you can modify it:

  1. Remove TypeScript-specific syntax, such as type annotations.
  2. Ensure that the useSharedValue and ref are used correctly without type definitions.

Here's the JavaScript version of the code:

import React, { useRef } from "react";
import { Dimensions, Text, View } from "react-native";
import { useSharedValue } from "react-native-reanimated";
import Carousel, { Pagination } from "react-native-reanimated-carousel";

const data = [...new Array(6).keys()];
const width = Dimensions.get("window").width;

function App() {
  const ref = useRef(null);
  const progress = useSharedValue(0);

  const onPressPagination = (index) => {
    ref.current?.scrollTo({
      count: index - progress.value,
      animated: true,
    });
  };

  return (
    <View style={{ flex: 1 }}>
      <Carousel
        ref={ref}
        width={width}
        height={width / 2}
        data={data}
        onProgressChange={progress}
        renderItem={({ index }) => (
          <View
            style={{
              flex: 1,
              borderWidth: 1,
              justifyContent: "center",
            }}
          >
            <Text style={{ textAlign: "center", fontSize: 30 }}>{index}</Text>
          </View>
        )}
      />
      <Pagination.Basic
        progress={progress}
        data={data}
        dotStyle={{ backgroundColor: "rgba(0,0,0,0.2)", borderRadius: 50 }}
        containerStyle={{ gap: 5, marginTop: 10 }}
        onPress={onPressPagination}
      />
    </View>
  );
}

export default App;

This should work for JavaScript. Make sure you have the correct versions of the dependencies installed, as mentioned in similar threads [1][2].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants