trying to build a new simple React component
I'm attempting to build a dropdown component as follows:
under src/components
/Making dropdown list
import React, { Component, PropTypes } from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Dropdown.scss';
// @withStyles(s)
class Dropdown extends Component {
static propTypes = {
maxNoItems: PropTypes.number
};
static defaultProps = {
maxNoItems: 3
};
render() {
return (
<div className="{s.root} {s.dropdown}">
<button className="{s.btn} {s.btn-default} {s.dropdown-toggle}" type="button" id="dropdownMenu1" >
Dropdown
<span className="{s.caret}"></span>
</button>
<ul className="{s.dropdown-menu}">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" className="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
);
}
}
export default withStyles(Dropdown, s);
My dropdown styling.
.root{
}
.btn{
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 14px;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
-ms-touch-action: manipulation;
touch-action: manipulation;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
}
.btn-default{
color: #333;
background-color: #fff;
border-color: #ccc;
}
.dropdown{
position: relative;
}
.caret{
margin-left: 0;
}
.dropdown-toggle{
float: left;
}
.dropdown-menu{
position: absolute;
top: 100%;
left: 0;
z-index: 1000;
display: none;
float: left;
min-width: 160px;
padding: 5px 0;
margin: 2px 0 0;
font-size: 14px;
text-align: left;
list-style: none;
background-color: #fff;
-webkit-background-clip: padding-box;
background-clip: padding-box;
border: 1px solid #ccc;
border: 1px solid rgba(0,0,0,.15);
border-radius: 4px;
-webkit-box-shadow: 0 6px 12px rgba(0,0,0,.175);
box-shadow: 0 6px 12px rgba(0,0,0,.175);
}
.dropdown-menu>li>a{
display: block;
padding: 3px 20px;
clear: both;
font-weight: 400;
line-height: 1.42857143;
color: #333;
white-space: nowrap;
}
And I want to test rendering it by importing on one of pages.
import React, { Component, PropTypes } from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './LoginPage.scss';
import Dropdown from '../Dropdown';
.......
.......
class LoginPage extends Component {
static contextTypes = {
onSetTitle: PropTypes.func.isRequired,
};
componentWillMount() {
this.context.onSetTitle(title);
}
render() {
return (
<div className={s.root}>
<div className={s.container}>
<h1>{title}</h1>
<p>...</p>
</div>
<Dropdown />
</div>
);
}
}
export default withStyles(LoginPage, s);
I can see the Dropdown html markup being rendered. However my own styling (as above) is not there. I want to know what I have done wrong? What am I missing here?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Hello, have you tried renaming your scss class names? Instead of dropdown-menu to dropdownMenu?
Hi @mzparulina
I tried that. It didn't work.
I went as far as in renaming my css classnames where there's a hyphen in them. Now my scss file turns out like so.
.root{
}
.btn{
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 14px;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
-ms-touch-action: manipulation;
touch-action: manipulation;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
}
.btnDefault{
color: #333;
background-color: #fff;
border-color: #ccc;
}
.dropdown{
position: relative;
}
.caret{
margin-left: 0;
}
.dropdownToggle{
float: left;
}
.dropdownMenu{
position: absolute;
top: 100%;
left: 0;
z-index: 1000;
display: none;
float: left;
min-width: 160px;
padding: 5px 0;
margin: 2px 0 0;
font-size: 14px;
text-align: left;
list-style: none;
background-color: #fff;
-webkit-background-clip: padding-box;
background-clip: padding-box;
border: 1px solid #ccc;
border: 1px solid rgba(0,0,0,.15);
border-radius: 4px;
-webkit-box-shadow: 0 6px 12px rgba(0,0,0,.175);
box-shadow: 0 6px 12px rgba(0,0,0,.175);
}
.dropdownMenu>li>a{
display: block;
padding: 3px 20px;
clear: both;
font-weight: 400;
line-height: 1.42857143;
color: #333;
white-space: nowrap;
}
Then renamed classnames in the react component. But the styling still never comes through..
@awongCM Why did you commented withStyles? Try to uncomment.
// @withStyles(s)
@arkeros If I uncommented withStyles, npm console log will complain and it says the following:
/home/andy/Projects/Javascript/myreactapp/node_modules/babel-runtime/helpers/classCallCheck.js:7
throw new TypeError("Cannot call a class as a function");
^
TypeError: Cannot call a class as a function
at exports.default (/home/andy/Projects/Javascript/myreactapp/node_modules/babel-runtime/helpers/classCallCheck.js:7:11)
at Styles (/home/andy/Projects/Javascript/myreactapp/node_modules/isomorphic-style-loader/lib/withStyles.js:44:36)
at Object.<anonymous> (/home/andy/Projects/Javascript/myreactapp/build/webpack:/src/components/Dropdown/Dropdown.js:7:2)
Originally I used the same code structure from React Text component. But the withstyles annotation doesn't work for my component. I didn't understand why it wouldn't at the time.
@awongCM could you put this on a repo/fork for us to reproduce?
@goldensunliu I commited my changes. However I realised that I did not fork this from the master branch of this repo. It's a clone of the master branch.
Is it okay for me to push my changes to remote master branch?
Check out #443
I finally managed to get the styles coming through on the page now.
It was simply because my JSX portion was wrong when dealing with CSS classes.
From my Dropdown.js
.....
render() {
return (
<div className={s.root + " " + s.dropdown}>
<button className={s.btn + " " + s.btnDefault + " " + s.dropdownToggle } type="button" id="dropdownMenu" >
Dropdown
<span className={s.caret}></span>
</button>
<ul className={s.dropdownMenu}>
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" className="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
);
}
It was because in the previous code, I used double quotes surrounding my isomorphic s variable. And because JSX parses treats it as a type string, it will return them as literal string - hence why no styles came through!
Which is great news in the sense.
However that still doesn't explain why I get the TypeError Cannot call a class as a function in the first place when trying to use withStyles annotation. This is the working code below.
//Making dropdown list
import React, { Component, PropTypes } from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Dropdown.scss';
class Dropdown extends Component {
static propTypes = {
maxNoItems: PropTypes.number
};
static defaultProps = {
maxNoItems: 3
};
render() {
return (
<div className={s.root + " " + s.dropdown}>
<button className={s.btn + " " + s.btnDefault + " " + s.dropdownToggle } type="button" id="dropdownMenu" >
Dropdown
<span className={s.caret}></span>
</button>
<ul className={s.dropdownMenu}>
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" className="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
);
}
}
export default withStyles(Dropdown, s);
@awongCM thank you very much for crating this issue! Unfortunately, we have close it due to inactivity. Feel free to re-open it or join our Discord channel for discussion.
NOTE: The main branch has been updated with React Starter Kit v2, using JAM-style architecture.