I am using react-rails and browserify. In the application.js I use
// require react
// require react-ujs
Also I use tcomb-form by using
var t=require("tcomb-form")
As the result I saw reactjs is included twice in my asset pipeline.
How should I correctly use reactjs with rails?
EDIT:
by the way, if you see this error in your console, probably you are having the same issue with reactjs got included twice.
[Error] 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.
The reason why this happens is an intentional feature of the React. StrictMode . It only happens in development mode and should help to find accidental side effects in the render phase.
There are two ways to use React with Ruby on Rails. The first is to build two separate, standalone apps, with the React app on a different repository and communicating with the backend via an API. This can simply be achieved by creating both apps separately using Create React App and the Rails CLI.
I found that react was being referenced twice in
app/assets/javascripts/server_rendering.js
When I removed the line
//= require react-server
it fixed my issues and got rid of the addComponentAsRefTo() error. Please bear in mind that I have not tried to deploy the app yet, but this worked for getting it to crud.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With