Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-bootstrap elements does not have styling

I am working on a webapp using react.js and react-router and webpack. I would like to use react-bootstrap for styling but my navbar does not appear to be styled as shown here Navbar

this is my react-bootstrap code

<Navbar inverse fixedTop>
            
            <Navbar.Collapse>
            <Nav bsStyle="pills">
              <NavItem  href="#"><Link to="/">{this.state.navMenu[0] }</Link></NavItem>
              <NavItem  href="#"><Link to="/utilities">{this.state.navMenu[3] }</Link></NavItem>
              <NavItem  href="#"><Link to="/main">{this.state.navMenu[4] }</Link></NavItem>
            </Nav>             
            </Navbar.Collapse>
          </Navbar>

these are the dependencies

"devDependencies": {
"babel-cli": "^6.4.0",
"babel-core": "^6.4.0",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"css-loader": "^0.23.1",
"file-loader": "^0.8.5",
"history": "^1.17.0",
"react": "^0.14.6",
"react-dom": "^0.14.6",
"style-loader": "^0.13.0",
"webpack": "^1.12.11",
"webpack-dev-server": "^1.14.1"},

  "dependencies": {
    "bootstrap": "^3.3.6",
    "imports-loader": "^0.6.5",
    "react": "^0.14.6",
    "react-bootstrap": "^0.28.2",
    "react-router": "^2.0.0-rc5"
  }

and this is the webpack.config file

var webpack = require('webpack');  
module.exports = {  
    entry: [
      'babel-polyfill',
      'webpack/hot/only-dev-server',
      "./app.js",
    ],
    output: {
        path: __dirname + '/build',
        filename: "bundle.js"
    },
    module: {
        loaders: [
            { test: /\.jsx?$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/},
            { test: [/\.jsx?$/, /\.es6$/], exclude: /node_modules/, loader: 'babel-loader'  },
            { test: /\.css$/, loader: 'style!css' },
        ]
    },
     resolve: {
    // you can now require('file') instead of require('file.coffee')
    extensions: ['', '.js', '.json', '.coffee'] 
    },
    plugins: [
      new webpack.NoErrorsPlugin(),
      
    ]

};

Ive tried to require 'bootstrap' but I get the following error: Uncaught ReferenceError: jQuery is not defined

any help is greatly appreciated please.

like image 240
Suemayah Eldursi Avatar asked Jan 30 '16 22:01

Suemayah Eldursi


People also ask

Can you use styled components with React Bootstrap?

Bootstrap Styled is a front-end ecosystem for React made with Bootstrap 4 philosophy, using the power of css-in-js thanks to styled-components. It offer a community an ecosystem of tools, components and variables to create standardized, sharable and highly customizable front-end modules.

Can you add style to React component?

Sharing styles across many React components The style objects and the components do not have to be in the same file. We can create a separate . js file for our styles, export these styles, and then import them into the component where we want to use them.


2 Answers

You shouldn't include bootstrap, because it will include in your project all bootstrap js files (they have as dependency jquery - therefore you get error). You don't need bootstrap js files, because you use react-bootstrap instead.

To include all css files from bootstrap write :

import style from 'bootstrap/dist/css/bootstrap.css';

After that you will also have problem with additional loaders. This is because, bootstrap styles use other files too.

Install:

npm install --save-dev url-loader

And you have to add to webpack configuration loader:

{
  test: /\.(png|woff|woff2|eot|ttf|svg)$/,
  loader: 'url-loader?limit=100000'
},

In style-loader documentation page, author recommend to use style-loader with css-loader. Change this:

{ test: /\.css$/, loader: 'style!css' },
like image 199
Krzysztof Sztompka Avatar answered Oct 13 '22 02:10

Krzysztof Sztompka


An update some may find useful: make sure you install bootstrap v3.3.7 (npm install --save [email protected]) and not the newest version 4 because react-bootstrap targets the CSS in v3. Many of the styles, such as those for the navbar, don't exist in the CSS for v4.

like image 36
BWildish Avatar answered Oct 13 '22 00:10

BWildish