Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use bootstrap in react with rails

I have tried using npm package reactstrap. My components:

import { Col, Button, Form, FormGroup, Label, Input, FormText } from 'reactstrap';

I'm getting the following error:

ExecJS::ProgramError at /dir/xyz
TypeError: require is not a function
like image 728
Mukarram Ali Avatar asked Mar 27 '18 15:03

Mukarram Ali


People also ask

Can React and Bootstrap be used together?

Bootstrap can be used directly on elements and components in your React app by applying the built-in classes as you would any other class.


1 Answers

The webpacker-gem is the way to go for react + rails, not sprockets, which is ok for jQuery style JS, but not for complicated ES6-based stacks.

From the README:

To use Webpacker with React, create a new Rails 5.1+ app using --webpack=react option:

# Rails 5.1+
rails new myapp --webpack=react

(or run bundle exec rails webpacker:install:react in a existing Rails app already setup with Webpacker).

The installer will add all relevant dependencies using Yarn, changes to the configuration files, and an example React component to your project in app/javascript/packs so that you can experiment with React right away.

Note that you'll be using yarn from then on, instead of npm.

like image 84
murb Avatar answered Sep 22 '22 11:09

murb