Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use react-bootstrap with postcss-module and react-css-module?

Below is my Greeter.jsx file:

import React, {Component} from 'react';
import cssModules from 'react-css-modules';
import {Button} from 'react-bootstrap';
import styles from './Greeter.css';

const option = {
  'allowMultiple': true
};

class Greeter extends Component{
  render() {
    return (
      <div styleName='root root-child'>
        <h1>Welcome to React Devops.</h1>

        <p styleName="para">This is an amazing para.</p>
        <p>Hot module reload.</p>

        <Button bsStyle="primary">Test</Button>
      </div>
    );
  }
}

export default cssModules(Greeter, styles, option);

Below is my main.js file:

import React from 'react';
import {render} from 'react-dom';
import Greeter from './Greeter';
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap/dist/css/bootstrap-theme.min.css';

import './main.css';

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

I use postcss-modules and react-css-modules to isolate selectors within components due to which, when the file loads, the class name becomes something like _3lmHzYQ1tO8xPJFY8ewQax.

Example:

<div class="_3lmHzYQ1tO8xPJFY8ewQax _32vj3squi8uWPfEu4ZzyBZ" data-reactid=".0"></div>

Below is how react-bootstrap would give me the output:

<button class="btn btn-primary"></button>

which has not been isolated as I use bsStyle (react-bootstrap) rather than styleName(react-css-modules) and hence I cannot apply the bootstrap css style to the element.

Is there a way through which I can use react-bootstrap by isolating its class to match the output that postcss-modules generate?

Thanks in anticipation.

like image 970
shet_tayyy Avatar asked Mar 01 '16 11:03

shet_tayyy


People also ask

Can I use CSS with React Bootstrap?

Style & Bootstrap Stylesheet. React Bootstrap does not come with any CSS. You will need to include some sort of stylesheet for your components to appear as they should on the screen.

Can we use both CSS and SCSS in React?

Introduction: We can use SASS in React using a package called node-sass. Using node-sass we can just create sass files and use them as the normal CSS files in our React application and node-sass will take care of compiling sass files. Modules: To compile sass code, node-sass can be used.

Does CSS modules work with SCSS?

Why CSS Modules with SCSS. CSS Modules allow you to have modular styling in your components. This means no more conflicts of classNames within your application. Meanwhile, SCSS is a CSS Pre- processor that enables you with capabilities such as mix-ins, global variables, nesting and so on.

Can I use Bootstrap and React Bootstrap together?

Install React Bootstrap Package The other method to add bootstrap in your React component is adding a package with the inbuilt bootstrap component. These are designed to work with your React application components. Below is the name of two popular packages. Both are good choices for using Bootstrap with React apps.

What is react CSS module?

React - The Complete Guide (incl Hooks, React Router, Redux) What is Css module? A css module is a css file that contains all class names and animation names are scoped by default it means the styles are only applied to the particular component.

How to use Bootstrap with react?

How to Use Bootstrap with React? 1 1. Using Bootstrap CDN. This is one of the easiest ways to use bootstrap in your React app. The best thing about bootstrap CDN is no requirement for ... 2 2. Import Bootstrap as a Dependency. 3 3. Install React Bootstrap Package.

How to use CSS modules with create-react-app?

The react apps we created using create-react-app are already comes with css-modules support. Now open your react-cssmodules folder in your favorite code editor and navigate to the src folder then delete App.css file. To use css modules first we need to create one let’s create a new css module called App.module.css and add the below class names.

How do I import a CSS file into a React component?

Great, now in your React component you should be able to import the CSS file directly: import React from 'react'; import './App.css'; const App = () => ( <div styleName="app"> Hello, world! </div> ); export default App; The styleName property is babel-plugin-react-css-modules’s replacement for className, but you get used to it pretty quickly.


1 Answers

I also faced same issue while using react-bootstrap with css-module. In my case it was Tabs component. It looks hacky though but worked for me. I wrapped the component with a class and within the class I write scss as follows

// myComponent.scss
.myComponent {
      :global {
        .nav-tabs {
           li {
             //my custom css for tabs switcher
           }
        }
      }
    }

and the MyComponent.js is

const tabsInstance = (
  <Tabs defaultActiveKey={1} animation={false} id='noanim-tab-example'>
    <Tab eventKey={1} title='Sign up'>Tab 1 content</Tab>
    <Tab eventKey={2} title='Login'>Tab 2 content</Tab>
  </Tabs>
)

export const MyComponent = () => (
  <div className={classes.myComponent}>
    { tabsInstance }
  </div>
)

export default MyComponent

Hope this is what you are looking for.

like image 142
Nur Rony Avatar answered Sep 25 '22 19:09

Nur Rony