Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid reactjs to be included twice in a rails and reactjs environment?

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.
like image 319
ziweizhou Avatar asked Apr 18 '15 15:04

ziweizhou


People also ask

Why component render 2 times react?

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.

How does react work with Rails?

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.


1 Answers

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.

like image 56
jesser360 Avatar answered Nov 15 '22 04:11

jesser360