I’ve configured my package.json with all the necessary dependencies and build scripts. When I run it locally using npm link, everything compiles, but I end up with an ‘Invalid hook call’ error. The types are imported correctly, so I’m suspecting it might be a React version conflict even though both projects are using the same version.
I’m searching for a reliable way to structure this for local development. Has anyone managed to set up a separate RTK Query API package without encountering this hook error? Any suggestions or best practices would be really helpful.
I’ve been down this road before, and it can be tricky. One approach that’s worked well for me is to export the API slice configuration separately from the actual API instance. This way, you’re not creating the API directly in your package, which can lead to those pesky hook errors.
This approach gives you more flexibility and avoids issues with React’s rules of hooks. Also, make sure you’re using peerDependencies for React and Redux-related packages in your package.json. This helps ensure version compatibility between your package and the consuming app.
Lastly, consider using a monorepo setup if you’re not already. It can simplify local development and version management across your projects.
I’ve encountered this issue before when developing reusable RTK Query packages. One approach that worked well for me was to create a custom hook within your package that initializes and returns the API slice. This way, you can encapsulate the API creation logic and export a function that consumers can use to integrate the API into their Redux store.
This method has helped me avoid hook-related errors and made the package more flexible for different environments. Remember to thoroughly test across different React and Redux versions to ensure compatibility.
hey liam, had similar issue. try using peerDependencies instead of regular dependencies for react and redux. also, make sure ur consuming app uses exact same versions. might help with that nasty hook error. good luck mate!