1

I found a generic open source react component on github that I want to modify and use in my own react project. That component is in a project using rollup as the bundler. It's a very simple component with only one file, src/index.tsx. Included in the project are a rollup.config.js and tsconfig.json.

I'd prefer to fork the project and modify it it's own repository and include it in my project as opposed to just copying the code into my project.

In order to modify the component I'd like to be able to see it in a html page as I make changes, as I would when working on a create-react-app project.

What is the typical development workflow when working in a react project that only includes components? How exactly can I preview changes?

I've tried making another react app as a sub-project within the component project and importing the bundled output of the parent project, although it compiled successfully is giving me this error, (I suspect the issue of duplicate react).

rosghub
  • 8,924
  • 4
  • 24
  • 37
  • Have you heard of storybook? https://storybook.js.org/ – Khalt Sep 18 '21 at 07:34
  • @Khalt it looks like storybook depends on using a framework like create-react-app. all I have is a single component I want to modify. While this looks like a good tool to use when starting from scratch I'm more interested in learning how react components are developed on their own without being part of a project if that makes sense. – rosghub Sep 18 '21 at 16:45
  • I don’t know your exact circumstances but I personally would include new components via an additional branch of the project. I think it’s best to also have some kind of staging for previewing and testing - but depending on the size of the project a local branch maybe sufficient. – moritzgvt Sep 19 '21 at 08:52

1 Answers1

0

Posting my solution in case it helps anyone.

What I wanted to do was visually see the changes I was making to the react component. Storybook is great for this but if the component is a github fork of a rollup bundle this may be useful.

What I ended up doing was clone the library and import it locally into my react project. For example if my react project was projects/react-app and my component was component-project, in projects/react-app run npm install ../component-project.

Then I linked both projects to a global version of react. This was necessary so both the react app and component bundle use the same version of react during development. Otherwise you get weird errors trying to use hooks.

I resolved this by following the steps in this answer:

1. In Your Application:
a) cd node_modules/react && npm link
b) cd node_modules/react-dom && npm link

2. In Your Library
a) npm link react
b) npm link react-dom

From there you can start your development server and watch for changes with rollup and see your changes as you make them in the react component library. After making the changes I wanted I published the library to github packages to continue using in my CD workflow.

rosghub
  • 8,924
  • 4
  • 24
  • 37