0

I have a library called, 'myLib' that has several react components, comp1,..,compn, that i want to be able to import to another project.

The folder structure goes as

|_Components
       |_ Comp1.js
       |_ Comp2.js
       |_ Comp3.js
|_index.js

Currently inside the index.js i have

exports.Comp1 = require('./components/comp1');
exports.Comp2 = require('./components/comp2');
exports.Comp3 = require('./components/comp3');

And inside the project i'm importing 'myLib', i'm doing so by

import myLib = require('myLib');

and to use the react components 'Comp1', inside the 'myApp' 's render(), i do

<myLib.Comp1></myLib.Comp1>

But i'm getting this error

Uncaught Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you're trying to add a ref to a component that doesn't have an owner (that is, was not created inside of another component's `render` method). Try rendering this component inside of a new top-level component which will hold the ref.

A little bit of googling suggest react is being referenced twice. Once inside the Comp1 and the 2nd time inside 'myApp' who's using Comp1.

My question is, am i doing this right? and how to fix the error i'm getting.

Edit:

After reading the bundle.js that comes from 'myApp' it does seem like react is being referenced twice. My question now is how do i get rid of it. I'm using browserify.

2 Answers 2

1

My question now is how do i get rid of it. I'm using browserify.

From : https://medium.com/@dan_abramov/two-weird-tricks-that-fix-react-7cf9bbdef375


Here’s just a few ways why this might have happened:

  • You installed a package that specifies React as a dependency, and later installed React;
  • You installed React, and later a package that specifies a different version of React as a dependency;
  • You’re using a global React you get from CDN, but installed a library from NPM that got its own copy of React;
  • You ran npm install inside some NPM dependency’s folder, thus installing its development dependencies that likely include React;
Sign up to request clarification or add additional context in comments.

Comments

0

I solved it by bundling my dependencies into a separate bundle stated here https://stackoverflow.com/a/29011560/2149419

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.