te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Css classname not applied to react component - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Css classname not applied to react component - Stack Overflow

programmeradmin3浏览0评论

I appreciate your time and help. I spent hours trying to figure this out can't seem to get to the bottom of it.

I have this react ponent:

import styles from './style.scss';

class ButtonComponent extends React.Component { 
  render() {
    return (
      <div className={styles.bunny}>
        hello world
      </div>
    );
  }
}

and scss file:

.bunny {
  display: block;
  margin-top:50px;
  margin-bottom:55px;
  background-color: blue;
}

When I load up my html, the div has no classname when I expect it to be <div class="bunny">

When I put the class name in the react ponent literally like this:

<div className="bunny"> 

then I can see the className in the browser.

What am I doing wrong?

Thank you so much in advance.

I appreciate your time and help. I spent hours trying to figure this out can't seem to get to the bottom of it.

I have this react ponent:

import styles from './style.scss';

class ButtonComponent extends React.Component { 
  render() {
    return (
      <div className={styles.bunny}>
        hello world
      </div>
    );
  }
}

and scss file:

.bunny {
  display: block;
  margin-top:50px;
  margin-bottom:55px;
  background-color: blue;
}

When I load up my html, the div has no classname when I expect it to be <div class="bunny">

When I put the class name in the react ponent literally like this:

<div className="bunny"> 

then I can see the className in the browser.

What am I doing wrong?

Thank you so much in advance.

Share Improve this question edited Feb 7, 2018 at 23:56 Mark C. 6,4604 gold badges36 silver badges74 bronze badges asked Feb 7, 2018 at 23:51 johnwick0831johnwick0831 9483 gold badges12 silver badges26 bronze badges 6
  • How are you using the css-loader inside webpack? – Mark C. Commented Feb 7, 2018 at 23:54
  • I'm using the react-boiler-plate project, this is what I have in the webpack-base.babel.js { // Preprocess our own .css files // This is the place to add your own loaders (e.g. sass/less etc.) // for a list of loaders, see https://webpack.js/loaders/#styling test: /\.(s*)css$/, exclude: /node_modules/, use: ['style-loader', 'css-loader', 'sass-loader'], }, Not sure if this helps, can you be more specific? – johnwick0831 Commented Feb 7, 2018 at 23:55
  • Importing css or sass files does not actually import any javascript object that you can use inside your js files as css files do not export anything. It just tells your module bundler (webpack) to include the css file in your bundle if it uses your ponent somewhere. Just import the file with import './style.scss' and then use your classes as strings: <div className="bunny">. Of course your html file will need to link the css file too. It's more like tellling webpack: "Hey webpack, this ponent uses classes from this sass file so include it in my bundle." – trixn Commented Feb 8, 2018 at 1:05
  • Hi Trixn, this is not desired as I do not want to hard code classnames in js files. I have seen this working before but I'm not sure what I have configured wrong myself. – johnwick0831 Commented Feb 8, 2018 at 4:38
  • @trixn that is simply not true. – Mark C. Commented Feb 8, 2018 at 14:14
 |  Show 1 more ment

7 Answers 7

Reset to default 2

Firstly, you need to guarantee it's defined with: console.log(styles) Im' not sure you import style.scss correctly.

You need to import your scss like... import './styles.scss';

Webpack will take care of bundling your styles so you can add the className as a string on your ponent.

<div className="bunny">Hello World</div>

there was a similar problem. It turned out that I named the style file not news.module.scss, but news.modules.scss. After renaming "modules" to "module" and then importing the file, className started working

What fixed this for me was to change the name of my scss file from style.scss to style.module.scss

For me it's because I use monjs's require to load an es module.

I had different problem, that is not mentioned here, but that causes the same behavior (the class name is just not added to the output): Make sure you use a valid css identifier for your class name.

I used snake-case:

.spanning-grid-item {
  flex-grow: 1;
}

But I should have used camelCase instead:

.spanningGridItem {
  flex-grow: 1;
}
module.exports = {
  entry: __dirname + '/index.js',
  output: {
    publicPath: '/',
    filename: './bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel',
        query: {
          presets: ['es2015', 'stage-0', 'react']
        }
      },
      {
        test: /\.css$/,
        loader: "style-loader!css-loader?modules"
      },
    ]
  }
};

Check if the module query parameter exists in your css-loader configuration. If not, add it, This means that the cssModel is opened, And your className will eventually be piled to a globally unique string

发布评论

评论列表(0)

  1. 暂无评论