Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Container, Row, Col - in React-Bootstrap not working

I'm currently attempting to learn React, and React-Bootstrap.

I'm attempting to make good use of the React-Bootstrap grid layout. I'm not sure if I'm implementing it incorrectly. My gut says I'm using an impropver version somewhere because as far as I can tell the "Container, Col, Row" functionality isn't working at all.

What could be the issue? I'm out of ideas.

Versions from package.json:

  "dependencies": {
    "bootstrap": "^4.3.1",
    "jquery": "^3.0.0",
    "react": "^16.8.4",
    "react-bootstrap": "^1.0.0-beta.6",
    "react-dom": "^16.8.4",
    "react-scripts": "2.1.8",
    "typescript": "^3.3.4000"

The package.json from "bootstrap" dir:

  "_from": "bootstrap@latest",
  "_id": "[email protected]",

The package.json from "react-bootstrap" dir:

  "_from": "react-bootstrap@^1.0.0-beta.6",
  "_id": "[email protected]",

Please note that I've also tried installing and using bootstrap@3 with no luck:

npm install bootstrap@3 --save npm i --save bootstrap@3

Major snippet from index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';

class Module extends React.Component
{
  constructor(props)
  {
    super(props);
  }

  clickHandler = (command) =>
  {
    // ... some handler code here
  }

  render()
  {
    return (
      <Container>
        <Row>
          <Col>
            <table>
              <tr>
                <th class="r1_header"> Header 1 </th>
                <th class="r1_header"> Header 2 </th>
                <th class="r1_header"> Header 3 </th>
              </tr>
              <tr>
                <td> <button/> </td> // some more button stuff here
                <td> <button/> </td>
                <td> <button/> </td>
              </tr>
              <tr>
                <td> <button/> </td>
                <td> <button/> </td>
                <td> <button/> </td>
              </tr>
              <tr>
                <td> <button/> </td>
                <td> <button/> </td> 
                <td> <button/> </td>
              </tr>
              <tr>
                <th class="r2_header"> Header 1 </th>
                <th class="r2_header"> Header 2 </th>
                <th class="r2_header"> Header 3 </th>
              </tr>
              <tr>
                <td> <button/> </td>
                <td> <button/> </td>
                <td> <button/> </td>
              </tr>
              <tr>
                <td> <button/> </td>
                <td> <button/> </td>
                <td> <button/> </td>
              </tr>
              <tr>
                <td> <button/> </td>
                <td> <button/> </td>
                <td> <button/> </td>
              </tr>
            </table>
          </Col>
          <Col>
            // another table here... should be aligned next to the
            // prev "col" horizontally but is going underneath (vertically)
          </Col>
        </Row>
      </Container>
    );
  }
}

* UPDATE *

Here is an MCVE...

codesandbox

The displayed text should be HelloWorld but instead its:

Hello
World
like image 975
Frank Avatar asked Mar 21 '19 20:03

Frank


People also ask

Can I use bootstrap columns in react?

Using native Bootstrap in a React project can often cause errors due to an incorrect initial setup. In order to make Bootstrap columns work together, you need to make sure that you have correctly imported and required the CSS file containing styles for row and col classes.

How to add a row and column in a bootstrap table?

In index.html copy and paste the path to the latest bootstrap style sheets: then you can import like this in your components import { Container, Row, Col } from 'react-bootstrap'; and use like this You can use grid and then specify the no of columns you want to give for table and no of columns you want to keep for other stuff out of 12.

Why are my row and Col Styles not working in Bootstrap?

This is because the default Bootstrap styles for row and col classes are not applied even though Bootstrap is installed in your project. The App component simply loads the JSX without any declared styles for the classes used. These styles are present inside a CSS file inside the node_modules folder where the entire Bootstrap library is installed.

Can I combine content and columns in a bootstrap grid layout?

When building grid layouts, all content goes in columns. The hierarchy of Bootstrap’s grid goes from container to row to column to your content. On rare occasions, you may combine content and column, but be aware there can be unintended consequences. Bootstrap includes predefined classes for creating fast, responsive layouts.


2 Answers

I've resolved the issue. It was a miss-step on my part. I missed a crucial part of the set up outlined on the react-bootstrap main regarding style sheets.

In index.html copy and paste the path to the latest bootstrap style sheets:

<link
  rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
  integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
  crossorigin="anonymous"
/>
like image 189
Frank Avatar answered Oct 21 '22 07:10

Frank


According to react documentation you have to install bootstrap first

npm Install react-bootstrap bootstrap

then you have to import this line in your index.js or App.js

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

then you can import like this in your components import { Container, Row, Col } from 'react-bootstrap'; and use like this

 <Container>
                <Row>
                    <Col>1</Col>
                    <Col>2</Col>
                    <Col>3</Col>
                </Row>
            </Container>
like image 23
B1zzle Avatar answered Oct 21 '22 05:10

B1zzle