Skip to content

2016

How to Use Webpack with React and Bootstrap

Hello everyone and welcome to "Continuous Improvement", the podcast where we explore various tips, tricks, and techniques to constantly improve our development skills. I'm your host, Victor, and today we're going to talk about setting up a project using Webpack, React, and Bootstrap without jQuery.

So, I recently had the experience of setting up a project with these technologies and it turned out to be a bit more time-consuming than I initially anticipated. But worry not! I've decided to share the steps I followed to save you from any possible headaches.

The first step is to install all the required dependencies. Open up your terminal and type in the following commands:

npm install react react-dom bootstrap react-bootstrap babel-preset-react --save
npm install webpack css-loader style-loader file-loader url-loader babel-core babel-loader babel-preset-es2015 --save-dev

Once you have installed the dependencies, it's time to move on to step two. In this step, we need to make some changes to the webpack.config.js file. Open it up and add the necessary loaders. Here's an example of how your webpack.config.js file might look:

var path = require('path');
var webpack = require('webpack');

module.exports = {
  entry: './main.js',
  output: { path: __dirname, filename: 'bundle.js' },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          presets: ['es2015', 'react']
        }
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"]
      },
      {
        test: /\.png$/,
        loader: "url-loader?limit=100000"
      },
      // ... more loaders ...
    ]
  },
};

Great! Now that we have our loaders set up, let's move on to step three. In this step, you need to import the Bootstrap CSS into your main.js file. This can be done with a simple import statement, like this:

import App from './app.jsx';
import Bootstrap from 'bootstrap/dist/css/bootstrap.css';

Finally, we're at step four! In this step, we'll import React Bootstrap components into our app.jsx file. Here's an example of how that might look:

import React from 'react';
import ReactDOM from 'react-dom';
import { Button } from 'react-bootstrap';

const buttonsInstance = (
  <Button>Click me!</Button>
);

ReactDOM.render(buttonsInstance, document.getElementById('here'));

Don't forget to include a div with the appropriate ID in your HTML file, as well as the bundle.js file. Here's an example of what your HTML file might look like:

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello React</title>
  </head>
  <body>
    <div id="here"></div>
    <script src="bundle.js"></script>
  </body>
</html>

And that's it! By following these steps, you should be able to set up a project using Webpack, React, and Bootstrap without the need for jQuery. But remember, continuous improvement is all about learning and adapting, so feel free to explore and experiment with different setups that suit your needs.

If you have any questions or need further assistance, don't hesitate to reach out. You can find me on Twitter, my handle is @VictorDev. Thanks for joining me on this episode of "Continuous Improvement". Stay tuned for more exciting topics in the world of development. Until next time, happy coding!

如何將Webpack與React和Bootstrap一起使用

今天,我正在設定一個使用Webpack、React和Bootstrap(不包括jQuery)的專案。原本看似簡單直接的任務結果卻花了我比預期多的時間,所以我決定將步驟記錄在下面:

步驟1:安裝所有依賴

首先,安裝所有需要的依賴:

npm install react react-dom bootstrap react-bootstrap babel-preset-react --save
npm install webpack css-loader style-loader file-loader url-loader babel-core babel-loader babel-preset-es2015 --save-dev

步驟2:在webpack.config.js中添加Loaders

接下來,在你的webpack.config.js文件中添加必要的loaders:

var path = require("path")
var webpack = require("webpack")

module.exports = {
  entry: "./main.js",
  output: { path: __dirname, filename: "bundle.js" },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          presets: ["es2015", "react"],
        },
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.png$/,
        loader: "url-loader?limit=100000",
      },
      {
        test: /\.jpg$/,
        loader: "file-loader",
      },
      {
        test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
        loader: "url-loader?limit=10000&mimetype=application/font-woff",
      },
      {
        test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
        loader: "url-loader?limit=10000&mimetype=application/octet-stream",
      },
      {
        test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
        loader: "file-loader",
      },
      {
        test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
        loader: "url-loader?limit=10000&mimetype=image/svg+xml",
      },
    ],
  },
}

步驟3:在main.js中引入Bootstrap CSS

在你的main.js文件(或者是你的進口文件)中,引入Bootstrap CSS:

import App from "./app.jsx"
import Bootstrap from "bootstrap/dist/css/bootstrap.css"

步驟4:在app.jsx中引入React Bootstrap

最後,在您的app.jsx中,導入React Bootstrap組件:

import React from "react"
import ReactDOM from "react-dom"
import { Button } from "react-bootstrap"

const buttonsInstance = <Button>Click me!</Button>

ReactDOM.render(buttonsInstance, document.getElementById("here"))

HTML設定

別忘了附上一個帶有適當ID的div,以及bundle.js

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello React</title>
  </head>
  <body>
    <div id="here"></div>
    <script src="bundle.js"></script>
  </body>
</html>

這就完成了!如果有任何問題,歡迎隨時向我提問。