5

I'm using the following code to generate a simple UI, and I'm trying to convert it to use Bootstrap.

This is my original code (using Skeleton);

render:function(){
        return (
            <div className="grocery-item row">
                <div className="six columns">
                    <h4 className={this.props.item.purchased ? "strikethrough" : "" }>
                        {this.props.item.name}
                    </h4>
                </div>
                <form onSubmit={this.togglePurchased} className="three columns">
                    <button className={this.props.item.purchased ? "btn btn-danger" : "button-primary"}>{this.props.item.purchased ? "unbuy" : "buy"}</button>
                </form>
                <form className="three columns" onSubmit={this.delete}>
                    <button>&times;</button>
                </form>
            </div>
        )
    }

I tried doing something like this;

render:function(){
        return (        
          <table class="table table-striped">
            <thead>
              <tr>
                <th>Column 1</th>
                <th>Columan 2</th>
                <th>Columan 3</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <h4 className={this.props.item.purchased ? "strikethrough" : "" }>{this.props.item.name}</h4>
                <form onSubmit={this.togglePurchased} className="three columns">
                  <button className={this.props.item.purchased ? "btn btn-danger" : "button-primary"}>{this.props.item.purchased ? "unbuy" : "buy"}</button>
                </form>
                <form className="three columns" onSubmit={this.delete}>
                    <button>&times;</button>
                </form>
              </tr>
            </tbody>
          </table>
        )
    }

But it's not working like I expect. I'm using react-express-examplar as my starting point.

How can I get Bootstrap tables working in my React app?

  • 2
    `` shouldn't you use `className` instead of `class` ?
    – Jacob Goh Sep 17 '16 at 12:48
  • @JacobGoh - thanks I've tried it out without success, can you please add your provided solution with the right context as answer ? –  Sep 17 '16 at 14:41
  • I know reactjs but not bower. Unable to help you further. Btw, I noticed that you missed the ending table tag in your code – Jacob Goh Sep 17 '16 at 14:46
  • Another suggestion, I like to write html code 1st, make sure that it work, and only then convert the html to jsx using http://magic.reactjs.net/htmltojsx.htm . This helps me avoid jsx syntax error completely – Jacob Goh Sep 17 '16 at 14:53
  • 1
    I see many things done wrong and don't know if you have made mistake writing the post or it is actually a mistake. Can you provide a jsfiddle? – Lyubomir Sep 20 '16 at 00:06
  • @leo - You can clone the repo and see all the code which is working , I use it as-is and try to modify it to use different UI, i.e CRUD table using bootstrap theme ... –  Sep 20 '16 at 18:32
  • have you tried react-bootstrap package? (https://react-bootstrap.github.io/) – vijayst Sep 23 '16 at 12:40

3 Answers3

0

I think you'll have an easier time if you start your app using create-react-app. They even give instructions on how to use Bootstrap with React.

You said you're new to React. There are a lot of moving parts that can be overwhelming to figure out at first. Using create-react-app will take out a lot of the learning curve and make it a lot easier for you to get started with as little friction as possible.

Good luck!

Jo Sprague
  • 16,523
  • 10
  • 42
  • 62
-1

From my understanding, you want the name and buy/unbuy buttons to come as a row , under respective columns. In the tr tag, give each html part as a td tag Please refer below html snippet:

 <td>
  <h4 class="strikethrough">
                    name
  </h4>
</td>
<td>
  <form class="three columns">
      <button class="btn btn-info">buy</button>
  </form>
</td>
<td>
  <form class="three columns">
      <button>&times;</button>
  </form>
</td>

Please try to modify the html in your component render method, similarly.

bhattshru
  • 29
  • 3
  • Thanks Already try it but not working for me..since I new to react I cannot put the finger and say what is the issue, I use the Git repo as is and try to change the theme –  Sep 20 '16 at 18:32
-1

You added table to wrong Component. It must be on GroceryItemList.jsx

Caution: Form is not allowed inside table. I'm just providing the structure and UI and code is not tested.

::: structure should something like-

GroceryItemList.jsx Component::::

render:function(){
        return (
            <div>
                <table className="table">
                    <thead>
                      <tr>
                        <th>Column 1</th>
                        <th>Columan 2</th>
                        <th>Columan 3</th>
                      </tr>
                    </thead>
                    <tbody>
                        {this.props.items.map((item,index)=>{
                            return (
                                <GroceryItem item={item} key={"item"+index} />
                            )
                        })}
                        <GroceryListAddItem />
                    </tbody>
                </table>
            </div>
        )
    }

GroceryItem.jsx Component::::

render:function(){
    return (
        <tr>
            <td>
                <h4 className={this.props.item.purchased ? "strikethrough" : "" }>
                    {this.props.item.name}
                </h4>
            </td>
            <td>
                <form onSubmit={this.togglePurchased}>
                    <button className={this.props.item.purchased ? "" : "button-primary"}>{this.props.item.purchased ? "unbuy" : "buy"}</button>
                </form>
            </td>
            <td>
                <form onSubmit={this.delete}>
                    <button>&times;</button>
                </form>
            </td>
        </tr>
    )
}
Fazal Rasel
  • 4,446
  • 2
  • 20
  • 31
  • Thank you.I've tried it and it seems that now the add button is not working and Im not able to add new item to the table,any idea? –  Sep 22 '16 at 08:32
  • I didn't vote down, i'll give you+1 since your code is partially working :) –  Sep 25 '16 at 06:34