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

后端 未结 8 1534
孤独总比滥情好
孤独总比滥情好 2020-12-08 02:38

According to a tweet by Dan Abramov, CSS modules support is there in create-react-app (CRA). One just needs to give extension of module.css to his stylesheets t

相关标签:
8条回答
  • 2020-12-08 03:15

    You are using version 1.1.4 , so CSS Modules doesn't work by default ... above version 2.0, it works default by changing the css file name to name.module.css.

    0 讨论(0)
  • 2020-12-08 03:16

    You do not need to eject.

    Create React App supports CSS Modules right out of the box as of version 2, which is now stable.

    Upgrade to v2 (react-scripts@latest) by running yarn upgrade react-scripts@latest or npm install --save react-scripts@latest.

    You just have to create a file with the extension .module.css

    For example:

    .myStyle {
      color: #fff
    }
    

    Then you can use it like so:

    import React from 'react'
    import styles from './mycssmodule.module.css'
    
    export default () => <div className={styles.myStyle}>We are styled!</div>
    
    0 讨论(0)
提交回复
热议问题