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

Feature/Docs: Add a explained documentation to the Ref section in Props page. #580

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions example/website/pages/props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,89 @@ Slide direction

## Ref

By using these methods, remember you need to reference the component using [React useRef()](https://react.dev/reference/react/useRef).

**JavaScript**

```js
const ref = React.useRef(null)
```

If you're using **TypeScript**:

You need to import:

```ts
import type { ICarouselInstance } from "react-native-reanimated-carousel";
```

and then:

```ts
const ref = React.useRef<ICarouselInstance>(null);
```

Now, you only need to pass the ref to the Carousel component:

```js
<Carousel ref={ref} (...) />;
```

And now you can use these methods throughout your component. Here's an example of implementing a button to go to the next slide:

```tsx
import React from "react";
import Carousel from "react-native-reanimated-carousel";
import type { ICarouselInstance } from "react-native-reanimated-carousel";
import { Button, Text, View } from "react-native";

// 1. Create a data array with the slides
const data = [
{
title: "Slide 1",
content: "Slide 1 Content",
},
{
title: "Slide 2",
content: "Slide 2 Content",
},
{
title: "Slide 3",
content: "Slide 3 Content",
},
];

const Example = () => {
const ref = React.useRef<ICarouselInstance>(null); // 2. Create a ref for the Carousel component

return (
<View>
{/* 3. Add the Carousel component with the ref */}
<Carousel
ref={ref}
data={data}
width={300} // 4. Add the required "width" prop
renderItem={({ item }) => (
<View>
<Text>{item.title}</Text>
<Text>{item.content}</Text>
</View>
)}
/>
{/* 5. Add a button to trigger the next slide */}
<Button
title="Next"
onPress={() => {
ref.current?.next(); // 6. Call the "next" method on the ref
}}
/>
</View>
);
};

export default Example;
```

### `prev`

Scroll to previous item, it takes one optional argument \(count\), which allows you to specify how many items to cross
Expand Down