Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 204
Sasha Kos Avatar asked Aug 08 '18 13:08

Sasha Kos


People also ask

What is emotion in react?

Emotion is a library designed for writing css styles with JavaScript. It provides powerful and predictable style composition in addition to a great developer experience with features such as source maps, labels, and testing utilities. Both string and object styles are supported.

Why external CSS is not working in react?

This error is generated because the compiler is only able to import files from the src folder. Here, the CSS file is saved outside the src folder, so the compiler failed to import it. To make this code work, you just have to save the CSS file inside the src folder.

How do you add emotions to react app?

There are lots of ways to use Emotion, if you're using React, the easiest way to get started is to use the @emotion/react package. If you're not using React, you should use the @emotion/css package. To use it, import what you need, for example use the css prop to create class names with styles. Some hotpink text.

Why do we use emotions?

Emotions can play an important role in how you think and behave. The emotions you feel each day can compel you to take action and influence the decisions you make about your life, both large and small.


1 Answers

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'));
like image 142
Mohit Tilwani Avatar answered Sep 21 '22 21:09

Mohit Tilwani