How to make a react project as library and include inside another react app?

Viewed 117

I am making a a big react app where, I am trying to split the sections into separate components.

Current Scenario:

I have two react projects

  • Stepper Project
  • Main Project

The reason I call it as a stepper project is because it consists of stepper wizard component with 5 steps. (You can refer code in the below working example)

I need to integrate the Stepper Project inside the Main Project.

Working example of Stepper Project:

const { useState, useEffect, useMemo } = React;
const cx = classNames;

function range(a, b) {
  return new Array(Math.abs(a - b) + 1).fill(a).map((v, i) => v + i);
}

function Stepper({ steps, activeStep, children }) {
  const count = steps.length;
  const listOfNum = useMemo(() => range(1, count), [count]);
  const tracker = useMemo(() => {
    let highestStep = 0;

    function hasVisited(step) {
      return highestStep >= step;
    }

    function addToBackLog(step) {
      if (step > highestStep) highestStep = step;
    }

    return {
      hasVisited,
      addToBackLog,
      getHighestStep() {
        return highestStep;
      },
    };
  }, []);

  tracker.addToBackLog(activeStep);

  const noop = () => {};

  const prevStep = steps[activeStep - 2];
  const currentStep = steps[activeStep - 1];
  const nextStep = steps[activeStep];

  return (
    <div>
      <div>
        {" "}
        {listOfNum.map((num, i) => {
          const isActive = activeStep == num;
          const isVisited = tracker.hasVisited(num);
          const isClickable = num <= tracker.getHighestStep() + 1 || isVisited;
          return (
            <div
              key={num}
              className={cx("circle", {
                active: isActive,
                visited: isVisited,
                clickable: isClickable,
              })}
              onClick={isClickable ? steps[i].onClick : noop}
            >
              {num}{" "}
            </div>
          );
        })}{" "}
      </div>{" "}
      <h2> {currentStep && currentStep.title} </h2> <div> {children} </div>{" "}
      <div className="footer">
        {" "}
        {prevStep ? (
          <button onClick={prevStep.onClick}> prev </button>
        ) : null}{" "}
        {nextStep ? <button onClick={nextStep.onClick}> next </button> : null}{" "}
      </div>{" "}
    </div>
  );
}

function App() {
  const [currentPage, setCurrentPage] = useState(1);
  const sections = [
    {
      title: "Un",
      onClick: () => setCurrentPage(1),
    },
    {
      title: "Deux",
      onClick: () => setCurrentPage(2),
    },
    {
      title: "Trois",
      onClick: () => setCurrentPage(3),
    },
    {
      title: "Quatre",
      onClick: () => setCurrentPage(4),
    },
    {
      title: "Cinq",
      onClick: () => setCurrentPage(5),
    },
  ];

  return (
    <Stepper steps={sections} activeStep={currentPage}>
      I 'm page {currentPage}{" "}
    </Stepper>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
body {
  color: #0f0035;
  padding-bottom: 2rem;
}

.circle {
  display: inline-flex;
  height: 2em;
  width: 2em;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background-color: lightgrey;
  margin: 0 0.5em;
  color: white;
  cursor: not-allowed;
}

.active {
  background-color: rgba(50, 50, 250) !important;
}

.visited {
  background-color: rgba(50, 50, 250, 0.5);
}

.clickable {
  cursor: pointer;
}

.footer {
  margin-top: 1em;
  display: flex;
  justify-content: space-between;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/classnames/2.2.6/index.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script>
<div id="root"></div>

And this above project has a stepper and I need to implement this exact project/library/component (I am not aware of correct term and I am sorry for that) inside the main project.

Working example of Main Project

function App() { 
 return (
   <h1> How to implement the stepper here in this main project?</h1>
 )
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/classnames/2.2.6/index.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

Requirement:

How to use the stepper project inside the Main project?

Please don't provide solution of copying the code from stepper and pasting it inside Main project.

I am in the need of dynamic solution where may be the stepper wizard converted to a library and include it inside main project.

I have already tried with the help of this question https://stackoverflow.com/a/65422780/7785337

But I am getting error because my app has src/index.js as react jsx code..

0 Answers
Related