Search code examples
reactjsemotion

Composition of react-emotion and external css (eg. bootstrap) classes


Is there a way to add external (eg. bootstrap) classes along with react-emotion styling?

import React, { Component } from 'react';
import { css } from 'react-emotion';

const MyStyle = css`
  STYLE
`
export default class MyComponent extends Component {
  render() {
    return (<button className={css`${MyStyle}` /* add in some way `btn btn-default` */ }>Text</button>);
  }
}

Thanks!


Solution

  • Yes you can. Below is the link where i had made a small example, the font colour is coming from react-emotion and background colour is coming from bootstrap.

    import React, { Component } from 'react';
    import { render } from 'react-dom';
    import 'bootstrap/dist/css/bootstrap.min.css';
    
    import styled, { css } from 'react-emotion'
    
    const myStyle = css`
      color: rebeccapurple;
      `;
    
    class App extends Component {
      constructor() {
        super();
        this.state = {
          name: 'React'
        };
      }
    
      render() {
        return (
          <div className={myStyle + ' bg-primary'}>Hello World</div>
        );
      }
    }
    
    render(<App />, document.getElementById('root'));