Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,094 questions

40,775 answers

573 users

How to render className style dynamically using bootstrap in React JS

1 Answer

0 votes
// counter.jsx

import React, { Component } from "react";

class Counter extends Component {
  state = {
    total: 996
  };

  render() {
    return (
      <React.Fragment>
        <span className={this.setStyle()}>
          Render Dynamically With bootstrap.css:{" "}
          {this.getTotal()}
        </span>
      </React.Fragment>
    );
  }

  setStyle() {
    let classes = "badge m-2 badge-";
    classes += this.state.total === 0 ? "warning" : "primary";
    return classes;
  }

  getTotal() {
    const { total } = this.state;
    return total === 0 ? "Zero" : total;
  }
}

export default Counter;
// index.js 

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import 'bootstrap/dist/css/bootstrap.css';
import Counter from './components/counter'


ReactDOM.render( 
  <Counter />,
  document.getElementById('root')
);

serviceWorker.unregister();


/*
run:
 
Render Dynamically With bootstrap.css: 996
 
*/

 





answered Mar 31, 2020 by avibootz
edited Mar 31, 2020 by avibootz

Related questions

1 answer 145 views
1 answer 114 views
1 answer 114 views
...