React component sharing inside two different projects

33 Views Asked by At

I want to use a common component in two different React projects. The component I prepared is as follows.

export const Carousel :FC<any> = (props) => {
  const [activeCategoryId, setActiveCategoryId] = useState(props.activeCategoryId)
  const [activeSlider, setActiveSlider] = useState(props.activeSliderIndex)
  const [swiper, setSwiper] = useState<any>({});



  const categoryItemOnClick = (e: any) => {
    setActiveCategoryId(e)
    const willChangeSlider = props.sliderData.findIndex((slider: any) =>slider.categoryId == e)
    swiper.slideTo(willChangeSlider, 700)
  }



  const sliderOnChange = (e: any) => {
    setActiveSlider(e.activeIndex)
    const willChangeSlider = props.sliderData[e.activeIndex].categoryId
    setActiveCategoryId(willChangeSlider as unknown as number)
  }

  return (
    <div>
      <div className="container relative flex flex-col gap-6">
        <CategoryWrapper itemOnClick={categoryItemOnClick} activeCategoryId={activeCategoryId} sliders={props.sliderData} categories={props.categoryData} />
        <Sliders sliderOnChange={sliderOnChange} activeSlider={activeSlider} sliders={props.sliderData} setSwiper={setSwiper} />
      </div>
    </div>
  )

}


export default Carousel

and in the other project I use it like this

import React from "react"
import {Carousel} from "my-common-components-project-folder"

export const HomePage = () => {
    return (
        <Carousel sliderData={[
      {
        uuid: "asdasdasdasd3213123",
        categoryId: "1",
        imagePath: "https://place-hold.it/1040x350?text=Category 1",
      },
      {
        uuid: "asdasdasd3123",
        categoryId: "1",
        imagePath: "https://place-hold.it/1040x350?text=Category 1.2",
      },
      {
        uuid: "asdasdasda",
        categoryId: "0",
        imagePath: "https://place-hold.it/1040x350?text=Category 0",
      },
      {
        uuid: "asdasd3123",
        categoryId: "2",
        imagePath: "https://place-hold.it/1040x350?text=Category 2",
      },
      {
        uuid: "asdasd31254",
        categoryId: "2",
        imagePath: "https://place-hold.it/1040x350?text=Category 2.2",
      },
      {
        uuid: "d3213123",
        categoryId: "3",
        type: 0, // media, video etc..
        imagePath: "https://place-hold.it/1040x350?text=Category 3",
      },
    ]}
             categoryData={[
               {name: "Category 0", id:"0"},
               {name: "Category 1", id:"1"},
               {name: "Category 2", id:"2"},
               {name: "Category 3", id:"3"},
             ]}
             activeCategoryId={2}
             activeSliderIndex={3} />
  )
}

export default HomePage

the application returns me an error message. the message is ;

`Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app`

both projects have the same versions

I need help with this

React component sharing inside two different projects

0

There are 0 best solutions below