Skip to content

Commit 0cc9ade

Browse files
ibrahimcesarclaude
andauthored
fix: React hooks null reference error (#211)
* fix: resolve React hooks null reference error and update demo dependencies This commit fixes the "Cannot read properties of null (reading 'useState')" error that occurred when using the library with Next.js. Changes: - Updated Vite config to explicitly set jsxRuntime: 'automatic' for proper JSX transformation - Updated demo to use Next.js 15.5.6 (from 14.2.33) for better React 19 support - Updated demo React to 19.0.0 (from 18.2.0) to match library peer dependencies - Added .eslintrc.json to demo for Next.js 15 ESLint compatibility The root cause was a combination of: 1. Implicit JSX runtime configuration in Vite 2. Outdated Next.js version with React version mismatch 3. Module resolution issues between the library build and Next.js runtime The fix ensures: - Proper React externalization in the library build - Compatible versions across the demo stack - Correct JSX transformation for library consumers Tested with: - npm run build (library): ✅ Successful - npm run build (demo): ✅ Successful (3/3 pages generated) - React hooks now properly resolve at runtime * docs: document local library development workflow in CONTRIBUTING.md Added comprehensive "Development Setup" section to explain: - Repository structure (library root + demo/ folder) - How the demo uses `file:..` to reference the local library - Complete getting started guide for new contributors - Step-by-step workflow for making and testing changes - Important notes about the local development approach This documentation helps contributors understand: - Why demo uses `file:..` instead of the published npm version - How to test library changes in the demo before publishing - The rebuild workflow required when modifying library source Related to the React hooks null error fix, this ensures contributors know how to properly set up and test the local development environment. * refactor: switch demo to use published npm package by default Changes the demo application to use the published "react-lite-youtube-embed" package instead of the local build, while documenting how contributors can switch to local development mode when needed. Changes: - Updated demo/package.json to use "react-lite-youtube-embed": "*" - Updated all imports from "@ibrahimcesar/react-lite-youtube-embed" to "react-lite-youtube-embed" - Updated next.config.js transpilePackages to use new package name - Rewrote CONTRIBUTING.md Development Setup section: - Demo uses published package by default (showcases what users install) - Added instructions for switching to local dev with "file:.." when testing changes - Emphasized reverting to published package before committing - Clearer workflow: getting started, running demo, local development, testing Why this change: - Demo showcases the actual package users will install from npm - Contributors can still test locally by temporarily using "file:.." - Ensures demo always represents the published user experience - Reduces confusion about which version is being used Local development workflow: 1. Change package.json to use "file:.." when testing library changes 2. Always revert to "react-lite-youtube-embed": "*" before committing --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent ee837b6 commit 0cc9ade

File tree

7 files changed

+754
-202
lines changed

7 files changed

+754
-202
lines changed

CONTRIBUTING.md

Lines changed: 114 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,146 @@
11
# Contributing
22

33
When contributing to this repository, please first discuss the change you wish to make via issue,
4-
email, or any other method with the owners of this repository before working on a change.
4+
email, or any other method with the owners of this repository before working on a change.
55

66
Please note we have a code of conduct, please follow it in all your interactions with the project.
77

8-
## Local Development and Testing
8+
## Development Setup
99

10-
### Setting Up Your Development Environment
10+
### Repository Structure
1111

12-
1. **Clone the repository and install dependencies:**
12+
This is a monorepo containing:
13+
- **Root** - The library package (`react-lite-youtube-embed`)
14+
- **demo/** - Next.js demo application showcasing the library
15+
16+
### Getting Started
17+
18+
1. **Clone the repository:**
19+
```bash
20+
git clone https://github.com/ibrahimcesar/react-lite-youtube-embed.git
21+
cd react-lite-youtube-embed
22+
```
23+
24+
2. **Install library dependencies:**
1325
```bash
1426
npm install
1527
```
1628

17-
2. **Build the library:**
29+
3. **Build the library:**
1830
```bash
1931
npm run build
2032
```
21-
This creates the distributable files in the `dist/` directory.
33+
This creates the `dist/` folder with the built library files.
34+
35+
4. **Run tests:**
36+
```bash
37+
npm test
38+
```
2239

23-
### Testing Your Changes in the Demo App
40+
5. **Check coverage:**
41+
```bash
42+
npm run coverage
43+
```
2444

25-
The demo app is already configured to use the local package via `"file:.."` in its `package.json`. Here's how to test your changes:
45+
### Running the Demo
2646

27-
#### One-Time Setup
47+
The demo application uses the **published npm package** by default:
2848

29-
1. **Navigate to the demo directory:**
30-
```bash
31-
cd demo
49+
```json
50+
// demo/package.json
51+
{
52+
"dependencies": {
53+
"react-lite-youtube-embed": "*"
54+
}
55+
}
56+
```
57+
58+
To run the demo:
59+
60+
```bash
61+
cd demo
62+
npm install
63+
npm run dev
64+
```
65+
66+
The demo will be available at `http://localhost:3000`
67+
68+
### Local Development Workflow
69+
70+
When you need to test library changes in the demo **before publishing**, switch to local development mode:
71+
72+
1. **Update demo/package.json:**
73+
```json
74+
{
75+
"dependencies": {
76+
"react-lite-youtube-embed": "file:.."
77+
}
78+
}
3279
```
3380

34-
2. **Install demo dependencies:**
81+
2. **Reinstall demo dependencies:**
3582
```bash
83+
cd demo
84+
rm -rf node_modules package-lock.json
3685
npm install
3786
```
3887
This will automatically link to your local package (in the parent directory).
3988

40-
#### Testing Workflow
89+
3. **Testing Workflow - Method 1: Build and Test (Recommended for Final Testing)**
4190

42-
**Method 1: Build and Test (Recommended for Final Testing)**
43-
44-
1. **Build the library** (from the root directory):
91+
a. **Build the library** (from the root directory):
4592
```bash
4693
npm run build
4794
```
4895

49-
2. **Start the demo app** (from the `demo/` directory):
96+
b. **Start the demo app:**
5097
```bash
5198
cd demo
5299
npm run dev
53100
```
54101

55-
3. **View the demo:**
56-
Open [http://localhost:3000](http://localhost:3000) in your browser.
57-
58-
4. **Make changes and rebuild:**
102+
c. **Make changes and rebuild:**
59103
- Make your changes to the library source files in `src/`
60104
- Run `npm run build` from the root directory
61105
- Refresh your browser to see the changes
62106

63-
**Method 2: Watch Mode (Recommended for Active Development)**
107+
4. **Testing Workflow - Method 2: Watch Mode (Recommended for Active Development)**
64108

65-
For a faster development cycle, you can use Vite's build watch mode:
109+
For a faster development cycle, you can use Vite's build watch mode:
66110

67-
1. **Start the build watcher** (from the root directory):
111+
a. **Start the build watcher** (from the root directory):
68112
```bash
69113
npm run build -- --watch
70114
```
71115
This rebuilds automatically whenever you save changes to source files.
72116

73-
2. **In a separate terminal, start the demo app:**
117+
b. **In a separate terminal, start the demo app:**
74118
```bash
75119
cd demo
76120
npm run dev
77121
```
78122

79-
3. **Develop with hot reload:**
123+
c. **Develop with hot reload:**
80124
- Edit files in `src/`
81125
- Vite automatically rebuilds the library
82126
- Next.js may require a manual browser refresh to pick up changes
83127

84-
#### Verifying Your Changes
128+
**Important:** Before committing, revert `demo/package.json` back to using the published package:
129+
```json
130+
{
131+
"dependencies": {
132+
"react-lite-youtube-embed": "*"
133+
}
134+
}
135+
```
136+
137+
### Verifying Your Changes
85138

86139
After making changes, always verify:
87140

88141
1. **Run tests:**
89142
```bash
90-
npm run test
143+
npm test
91144
```
92145

93146
2. **Check test coverage:**
@@ -96,29 +149,50 @@ After making changes, always verify:
96149
```
97150
Ensure coverage remains at 100%.
98151

99-
3. **Run linting:**
152+
3. **Build the library:**
153+
```bash
154+
npm run build
155+
```
156+
157+
4. **Run linting:**
100158
```bash
101159
npm run lint
102160
```
103161

104-
4. **Check formatting:**
162+
5. **Check formatting:**
105163
```bash
106164
npm run format:check
107165
```
108166

109-
5. **Type check:**
167+
6. **Type check:**
110168
```bash
111169
npm run type-check
112170
```
113171

114-
6. **Run all checks at once:**
172+
7. **Run all checks at once:**
115173
```bash
116174
npm run ci
117175
```
176+
This runs linting, type checking, tests, and builds the project—ensuring your changes will pass CI checks.
177+
178+
8. **Test the demo builds:**
179+
```bash
180+
cd demo
181+
npm run build
182+
```
183+
184+
### Working with CSS
185+
186+
If you modify styles in `src/lib/LiteYouTubeEmbed.css`:
187+
188+
1. Rebuild the library: `npm run build`
189+
2. The CSS is automatically copied to `dist/LiteYouTubeEmbed.css`
190+
3. Refresh the demo app to see style changes
118191

119-
#### Troubleshooting
192+
### Troubleshooting
120193

121194
**Changes not appearing in the demo?**
195+
- Ensure you switched to local development mode (`"file:.."` in demo/package.json)
122196
- Ensure you ran `npm run build` after making changes
123197
- Try clearing Next.js cache: `cd demo && rm -rf .next && npm run dev`
124198
- Hard refresh your browser (Ctrl+Shift+R or Cmd+Shift+R)
@@ -133,23 +207,13 @@ After making changes, always verify:
133207
- Check TypeScript errors: `npm run type-check`
134208
- Clear dist folder: `rm -rf dist && npm run build`
135209

136-
### Working with CSS
137-
138-
If you modify styles in `src/lib/LiteYouTubeEmbed.css`:
139-
140-
1. Rebuild the library: `npm run build`
141-
2. The CSS is automatically copied to `dist/LiteYouTubeEmbed.css`
142-
3. Refresh the demo app to see style changes
143-
144-
### Before Submitting Your PR
145-
146-
Make sure to run the full CI pipeline locally:
147-
148-
```bash
149-
npm run ci
150-
```
210+
### Development Tips
151211

152-
This runs linting, type checking, tests, and builds the project—ensuring your changes will pass CI checks.
212+
- **Library changes** require rebuilding (`npm run build`) to take effect in the demo
213+
- **Always revert** `demo/package.json` to use the published package before committing
214+
- The demo showcases the **published version** that users actually install
215+
- Use `file:..` only temporarily for local testing during development
216+
- Use watch mode (`npm run build -- --watch`) for faster iteration during active development
153217

154218
## Pull Request Guidelines
155219

demo/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["next/core-web-vitals"]
3+
}

demo/next.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ const nextConfig = {
1111
unoptimized: true,
1212
},
1313
// Transpile local package to ensure React imports work correctly
14-
transpilePackages: ['@ibrahimcesar/react-lite-youtube-embed'],
14+
transpilePackages: ['react-lite-youtube-embed'],
1515
}
1616
module.exports = nextConfig

0 commit comments

Comments
 (0)