1

This is an out-of-the-box Create React App install, and I have only done two things:

  1. npm install react-bootstrap bootstrap

  2. Changed App.js as follows:

    import logo from './logo.svg';
    import './App.css';
    import Button from 'react-bootstrap/Button';
    
    function App() {
    
      function buy() {
        alert('Clicked Buy');
      }
      function sell() {
        alert('Clicked Sell');
      }
    
      return (
        <div>
          <Button variant='success' onClick={buy}>
            Button1
          </Button>
          <Button variant='danger' onClick={sell}>
            Button2
          </Button>
        </div>
    
      );
    }
    
    export default App;
    

The buttons look the same as before I changed them from <button> to <Button variant=...:

enter image description here

3
  • "an out-of-the-box React install" - Create React App? Did you read react-bootstrap.github.io/getting-started/…? Commented Nov 15, 2020 at 18:43
  • yes - npx create-react-app Commented Nov 15, 2020 at 18:44
  • I read enough of it to install bootstrap I thought, guess I need to read the rest of it. Commented Nov 15, 2020 at 18:47

5 Answers 5

4

You have to import the css for the buttons to work

import 'bootstrap/dist/css/bootstrap.min.css';
Sign up to request clarification or add additional context in comments.

Comments

0

Add these lines to App.js, as per https://react-bootstrap.github.io/getting-started/introduction/#stylesheets

import 'bootstrap/dist/css/bootstrap.min.css';
<link
  rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
  integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk"
  crossorigin="anonymous"
/>

The link statement is not required.

Results after adding css:

enter image description here

Comments

0

You need to convert it to a ES6 class definition:

import 'bootstrap/dist/css/bootstrap.min.css';
import logo from './logo.svg';

import './App.css';
class App extends React.Component {
  constructor(props) {
    super(props);


    // This binding is necessary to make `this` work in the callback
    this.buy = this.buy.bind(this);
    this.sell= this.sell.bind(this);
  }

    function buy() {
    alert('Clicked Buy');
  }
  function sell() {
    alert('Clicked Sell');
  }


  return (
    <div>
      <Button variant='success' onClick={buy}>
        Button1
      </Button>
      <Button variant='danger' onClick={sell}>
        Button2
      </Button>
    </div>

  );
}

App()

1 Comment

That is absolutely not necessary, only the import of the CSS makes any difference.
0

Instead of npm install react-bootstrap , i did npm install react-bootstrap bootstrap as it says in the documentation.

Your code is right, but you should also add

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

in App.js

Comments

0

Follow these instructions

https://create-react-app.dev/docs/adding-bootstrap/

You need to add the dependencies in the package.json file (this is what yarn add react-bootstrap bootstrap will do).

Then yarn, to install the bootstrap-react node module.

Then you need to import of the bootstrap.css file. Add that import line to the top of the index.js file.

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

You can then see that it's imported when you inspect the html in devtools.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.