#How to use this React package on my Astro project?

4 messages · Page 1 of 1 (latest)

zinc jacinth
#

Use the code below as an example:

`import React, { useRef, useState } from "react";
// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";

// Import Swiper styles
import "swiper/css";

import "./styles.css";

export default function App() {
return (
<>
<Swiper className="mySwiper">
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
<SwiperSlide>Slide 5</SwiperSlide>
<SwiperSlide>Slide 6</SwiperSlide>
<SwiperSlide>Slide 7</SwiperSlide>
<SwiperSlide>Slide 8</SwiperSlide>
<SwiperSlide>Slide 9</SwiperSlide>
</Swiper>
</>
);
}
`

wary tartanBOT
#

Hi @zinc jacinth. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To use a React component in your Astro project, you first need to install the @astrojs/react integration. This can be done using the astro add command or manually.

Here are the steps to install @astrojs/react using the astro add command:

npx astro add react
# or
pnpm astro add react
# or
yarn astro add react

If you prefer to install manually, you can do so with the following commands:

npm install @astrojs/react
# or
pnpm add @astrojs/react
# or
yarn add @astrojs/react

You also need to install react and react-dom:

npm install react react-dom
# or
pnpm add react react-dom
# or
yarn add react react-dom

After installing the necessary packages, you need to apply the integration to your astro.config.* file using the integrations property:

import { defineConfig } from 'astro/config';
import react from '@astrojs/react';

export default defineConfig({
  // ...
  integrations: [react()],
});

Once you have the @astrojs/react integration installed and configured, you can use your React component in Astro. For example, if you have a React component named App, you can use it in your Astro file like this:

---
// Import your App component
import App from './App.jsx';
---

<!-- Use a client directive to load your app -->
<App client:load />

Please note that the path to the App component in the import statement should be adjusted based on your project structure.

#

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: