Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In create-react-app, adding Bootstrap 4?

Since create-react-app hides Webpack config without having to use eject, if I want to use something like reactstrap or react-bootstrap, should I eject or will I be fine without ejecting?

Just curious as to what point one would decide when they need to eject in order to use what they need? At this point in my app, I am merely prototyping, but it will go further with more dependencies and whatnot.

like image 380
Mark Avatar asked Jan 03 '18 00:01

Mark


People also ask

Can we use Bootstrap 4 in React?

Bootstrap 4 React Hello World Or, you can import Bootstrap in the React App using Javascript. For this method you will need to include Bootstrap in the dependencies section of the NPM package. json file. Bootstrap 4 JS depends on jQuery and popper.

Where should you add a bootstrap CDN link in a React application?

Add Bootstrap in React Using NPM: You must import a CSS file into your React app entry. If you generated your project using the create-react-app tool, open the src folder, and find the entry file paste. import 'bootstrap/dist/css/bootstrap. min.


2 Answers

When using create-react-app, you don't need to eject or edit webpack config for using react-bootstrap.

Install react-bootstrap

npm install --save react-bootstrap bootstrap@3

You have to import css separately by adding this to the top of your src/index.js

import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/css/bootstrap-theme.css';

Check out the documentation for using react-bootstrap.

Most dependencies can be added without changing webpack config. You can add packages and start using them.

If you're really concerned about the possibility of changing webpack config, I'd urge you to checkout roadhog. It's a configurable alternative of create-react-app

like image 151
sudo bangbang Avatar answered Sep 21 '22 18:09

sudo bangbang


It's easier now with the latest version of bootstrap: https://getbootstrap.com/docs/4.1/getting-started/webpack/

Install bootstrap and dependencies:

npm install bootstrap jquery popper.js --save

Then in your app entry point (index.js) import bootstrap and the css:

import 'bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';

Good to go!

EDIT

There's now a section in the create-react-app documentation: https://facebook.github.io/create-react-app/docs/adding-bootstrap#docsNav

They mention react-bootstrap and reactstrap if you'd rather use that, and you will not need to eject.

like image 24
mofojed Avatar answered Sep 17 '22 18:09

mofojed