Dynamically Add Classes with React classSet

https://manofhustle.com/2015/01/19/dynamically-add-classes-react-classset/

Note: React.addons.classSet is now deprecated and you should useclassnames. There is an explanation of how to use classnames at the end of the article.

Earlier today, I needed to add some classes to a link. One class was passed in through a prop, but the other class would be added based on a boolean condition.

It’s simple to access props within a React component, so my first crack at setting the classes looked something like this:

1
2
3
4
5
6
7
8
<li className={ this.props.className }>
     <a
         href={ this.props.href }
         onClick={ this.setLayoutFocus }
         className={ this.props.selected === this.props.className ? 'selected ' + this.props.className : this.props.className } >
             <span className="menu-link-text">{ this.props.label }</span>
     </a>
</li>

Eww… check out that nasty looking ternary.

Good thing for pull requests, because that one was denied pretty quickly. In the pull request feedback, Beau Lebens mentioned that there was a CSS utility included with React called classSet.

He mentioned that the React classSet utility would be helpful because I’d be able to build my classes string without having to have a bunch of conditional statements and string concatenation. #winning

So, I went Googling and figured out how to use the React classSet utility. Here’s the relevant documentation for using React’s classSet for class name manipulation.

Here’s an example of how the React classSet utility works from the documentation linked above.

1
2
3
4
5
6
7
8
9
10
render: function () {
     var cx = React.addons.classSet;
     var classes = cx({
         'message' : true ,
         'message-important' : this .props.isImportant,
         'message-read' : this .props.isRead
     });
     // same final string, but much cleaner
     return <div className={classes}>Great, I'll be there.</div>;
}

This is a simple example, but what about the case where the class is passed in via a prop as opposed to just being switched on or off by a boolean?

Second Try with React classSet

This try at adding classes via the React classSet utility allows us to add a class that is passed in via a prop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
render: function () {
     var classes = React.addons.classSet({
         'selected' : ( this .props.selected === this .props.className )
     });
 
     /*
      * Since the className changes from sidebar item to item,
      * we dynamically add this sidebar item's class as a key.
      */
     classes[ this .props.className ] = true ;
 
     return (
         <li className={ classes }>
             <a href={ this .props.href } onClick={ this .setLayoutFocus } >
                 <span className= "menu-link-text" >{ this .props.label }</span>
             </a>
         </li>
     );
}

Note that in this second try that we are dynamically appending our class that was passed in via the className prop to the classes object.

Then, when we call className={ classes } our string of classes is created.

Using classnames instead of classSet

React has deprecated React.addons.classSet and has suggested that developers use the classnames node module instead.

So, first things first, you’ll want to install classnames. You can usually do this in the root of your project, and you’ll use this command: npm install classnames.

Once you have installed classnames, you will then need to require it in your JSX file.

1
var classNames = require( 'classnames' );

Once we have required classnames, we should be able to simply swap outReact.addons.classSet with classNames. Even better though, sinceclassnames is so robust, we can just pass in this.props.className as an argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
render: function () {
     var classes = classNames( this .props.className, {
         'selected' : ( this .props.selected === this .props.className )
     } );
 
     return (
         <li className={ classes }>
             <a href={ this .props.href } onClick={ this .setLayoutFocus } >
                 <span className= "menu-link-text" >{ this .props.label }</span>
             </a>
         </li>
     );
}

https://manofhustle.com/2015/01/19/dynamically-add-classes-react-classset/ Note: React.addons.classSet is now deprecated and you should use classnames. There is an explanation of how to use classnames at the end of the article.

Earlier today, I needed to add some classes to a link. One class was passed in through a prop, but the other class would be added based on a boolean condition.

It’s simple to access props within a React component, so my first crack at setting the classes looked something like this:

1
2
3
4
5
6
7
8
<li className={ this.props.className }>
     <a
         href={ this.props.href }
         onClick={ this.setLayoutFocus }
         className={ this.props.selected === this.props.className ? 'selected ' + this.props.className : this.props.className } >
             <span className="menu-link-text">{ this.props.label }</span>
     </a>
</li>

Eww… check out that nasty looking ternary.

Good thing for pull requests, because that one was denied pretty quickly. In the pull request feedback, Beau Lebens mentioned that there was a CSS utility included with React called classSet.

He mentioned that the React classSet utility would be helpful because I’d be able to build my classes string without having to have a bunch of conditional statements and string concatenation. #winning

So, I went Googling and figured out how to use the React classSet utility. Here’s the relevant documentation for using React’s classSet for class name manipulation.

Here’s an example of how the React classSet utility works from the documentation linked above.

1
2
3
4
5
6
7
8
9
10
render: function () {
     var cx = React.addons.classSet;
     var classes = cx({
         'message' : true ,
         'message-important' : this .props.isImportant,
         'message-read' : this .props.isRead
     });
     // same final string, but much cleaner
     return <div className={classes}>Great, I'll be there.</div>;
}

This is a simple example, but what about the case where the class is passed in via a prop as opposed to just being switched on or off by a boolean?

Second Try with React classSet

This try at adding classes via the React classSet utility allows us to add a class that is passed in via a prop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
render: function () {
     var classes = React.addons.classSet({
         'selected' : ( this .props.selected === this .props.className )
     });
 
     /*
      * Since the className changes from sidebar item to item,
      * we dynamically add this sidebar item's class as a key.
      */
     classes[ this .props.className ] = true ;
 
     return (
         <li className={ classes }>
             <a href={ this .props.href } onClick={ this .setLayoutFocus } >
                 <span className= "menu-link-text" >{ this .props.label }</span>
             </a>
         </li>
     );
}

Note that in this second try that we are dynamically appending our class that was passed in via the className prop to the classes object.

Then, when we call className={ classes } our string of classes is created.

Using classnames instead of classSet

React has deprecated React.addons.classSet and has suggested that developers use the classnames node module instead.

So, first things first, you’ll want to install classnames. You can usually do this in the root of your project, and you’ll use this command: npm install classnames.

Once you have installed classnames, you will then need to require it in your JSX file.

1
var classNames = require( 'classnames' );

Once we have required classnames, we should be able to simply swap outReact.addons.classSet with classNames. Even better though, sinceclassnames is so robust, we can just pass in this.props.className as an argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
render: function () {
     var classes = classNames( this .props.className, {
         'selected' : ( this .props.selected === this .props.className )
     } );
 
     return (
         <li className={ classes }>
             <a href={ this .props.href } onClick={ this .setLayoutFocus } >
                 <span className= "menu-link-text" >{ this .props.label }</span>
             </a>
         </li>
     );
}

https://manofhustle.com/2015/01/19/dynamically-add-classes-react-classset/

Note: React.addons.classSet is now deprecated and you should useclassnames. There is an explanation of how to use classnames at the end of the article.

Earlier today, I needed to add some classes to a link. One class was passed in through a prop, but the other class would be added based on a boolean condition.

It’s simple to access props within a React component, so my first crack at setting the classes looked something like this:

1
2
3
4
5
6
7
8
<li className={ this.props.className }>
     <a
         href={ this.props.href }
         onClick={ this.setLayoutFocus }
         className={ this.props.selected === this.props.className ? 'selected ' + this.props.className : this.props.className } >
             <span className="menu-link-text">{ this.props.label }</span>
     </a>
</li>

Eww… check out that nasty looking ternary.

Good thing for pull requests, because that one was denied pretty quickly. In the pull request feedback, Beau Lebens mentioned that there was a CSS utility included with React called classSet.

He mentioned that the React classSet utility would be helpful because I’d be able to build my classes string without having to have a bunch of conditional statements and string concatenation. #winning

So, I went Googling and figured out how to use the React classSet utility. Here’s the relevant documentation for using React’s classSet for class name manipulation.

Here’s an example of how the React classSet utility works from the documentation linked above.

1
2
3
4
5
6
7
8
9
10
render: function () {
     var cx = React.addons.classSet;
     var classes = cx({
         'message' : true ,
         'message-important' : this .props.isImportant,
         'message-read' : this .props.isRead
     });
     // same final string, but much cleaner
     return <div className={classes}>Great, I'll be there.</div>;
}

This is a simple example, but what about the case where the class is passed in via a prop as opposed to just being switched on or off by a boolean?

Second Try with React classSet

This try at adding classes via the React classSet utility allows us to add a class that is passed in via a prop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
render: function () {
     var classes = React.addons.classSet({
         'selected' : ( this .props.selected === this .props.className )
     });
 
     /*
      * Since the className changes from sidebar item to item,
      * we dynamically add this sidebar item's class as a key.
      */
     classes[ this .props.className ] = true ;
 
     return (
         <li className={ classes }>
             <a href={ this .props.href } onClick={ this .setLayoutFocus } >
                 <span className= "menu-link-text" >{ this .props.label }</span>
             </a>
         </li>
     );
}

Note that in this second try that we are dynamically appending our class that was passed in via the className prop to the classes object.

Then, when we call className={ classes } our string of classes is created.

Using classnames instead of classSet

React has deprecated React.addons.classSet and has suggested that developers use the classnames node module instead.

So, first things first, you’ll want to install classnames. You can usually do this in the root of your project, and you’ll use this command: npm install classnames.

Once you have installed classnames, you will then need to require it in your JSX file.

1
var classNames = require( 'classnames' );

Once we have required classnames, we should be able to simply swap outReact.addons.classSet with classNames. Even better though, sinceclassnames is so robust, we can just pass in this.props.className as an argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
render: function () {
     var classes = classNames( this .props.className, {
         'selected' : ( this .props.selected === this .props.className )
     } );
 
     return (
         <li className={ classes }>
             <a href={ this .props.href } onClick={ this .setLayoutFocus } >
                 <span className= "menu-link-text" >{ this .props.label }</span>
             </a>
         </li>
     );
}

https://manofhustle.com/2015/01/19/dynamically-add-classes-react-classset/




Note: React.addons.classSet is now deprecated and you should useclassnames. There is an explanation of how to use classnames at the end of the article.

Earlier today, I needed to add some classes to a link. One class was passed in through a prop, but the other class would be added based on a boolean condition.

It’s simple to access props within a React component, so my first crack at setting the classes looked something like this:

1
2
3
4
5
6
7
8
<li className={ this.props.className }>
     <a
         href={ this.props.href }
         onClick={ this.setLayoutFocus }
         className={ this.props.selected === this.props.className ? 'selected ' + this.props.className : this.props.className } >
             <span className="menu-link-text">{ this.props.label }</span>
     </a>
</li>

Eww… check out that nasty looking ternary.

Good thing for pull requests, because that one was denied pretty quickly. In the pull request feedback, Beau Lebens mentioned that there was a CSS utility included with React called classSet.

He mentioned that the React classSet utility would be helpful because I’d be able to build my classes string without having to have a bunch of conditional statements and string concatenation. #winning

So, I went Googling and figured out how to use the React classSet utility. Here’s the relevant documentation for using React’s classSet for class name manipulation.

Here’s an example of how the React classSet utility works from the documentation linked above.

1
2
3
4
5
6
7
8
9
10
render: function () {
     var cx = React.addons.classSet;
     var classes = cx({
         'message' : true ,
         'message-important' : this .props.isImportant,
         'message-read' : this .props.isRead
     });
     // same final string, but much cleaner
     return <div className={classes}>Great, I'll be there.</div>;
}

This is a simple example, but what about the case where the class is passed in via a prop as opposed to just being switched on or off by a boolean?

Second Try with React classSet

This try at adding classes via the React classSet utility allows us to add a class that is passed in via a prop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
render: function () {
     var classes = React.addons.classSet({
         'selected' : ( this .props.selected === this .props.className )
     });
 
     /*
      * Since the className changes from sidebar item to item,
      * we dynamically add this sidebar item's class as a key.
      */
     classes[ this .props.className ] = true ;
 
     return (
         <li className={ classes }>
             <a href={ this .props.href } onClick={ this .setLayoutFocus } >
                 <span className= "menu-link-text" >{ this .props.label }</span>
             </a>
         </li>
     );
}

Note that in this second try that we are dynamically appending our class that was passed in via the className prop to the classes object.

Then, when we call className={ classes } our string of classes is created.

Using classnames instead of classSet

React has deprecated React.addons.classSet and has suggested that developers use the classnames node module instead.

So, first things first, you’ll want to install classnames. You can usually do this in the root of your project, and you’ll use this command: npm install classnames.

Once you have installed classnames, you will then need to require it in your JSX file.

1
var classNames = require( 'classnames' );

Once we have required classnames, we should be able to simply swap outReact.addons.classSet with classNames. Even better though, sinceclassnames is so robust, we can just pass in this.props.className as an argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
render: function () {
     var classes = classNames( this .props.className, {
         'selected' : ( this .props.selected === this .props.className )
     } );
 
     return (
         <li className={ classes }>
             <a href={ this .props.href } onClick={ this .setLayoutFocus } >
                 <span className= "menu-link-text" >{ this .props.label }</span>
             </a>
         </li>
     );
}

Note: React.addons.classSet is now deprecated and you should useclassnames. There is an explanation of how to use classnames at the end of the article.

Earlier today, I needed to add some classes to a link. One class was passed in through a prop, but the other class would be added based on a boolean condition.

It’s simple to access props within a React component, so my first crack at setting the classes looked something like this:

1
2
3
4
5
6
7
8
<li className={ this.props.className }>
     <a
         href={ this.props.href }
         onClick={ this.setLayoutFocus }
         className={ this.props.selected === this.props.className ? 'selected ' + this.props.className : this.props.className } >
             <span className="menu-link-text">{ this.props.label }</span>
     </a>
</li>

Eww… check out that nasty looking ternary.

Good thing for pull requests, because that one was denied pretty quickly. In the pull request feedback, Beau Lebens mentioned that there was a CSS utility included with React called classSet.

He mentioned that the React classSet utility would be helpful because I’d be able to build my classes string without having to have a bunch of conditional statements and string concatenation. #winning

So, I went Googling and figured out how to use the React classSet utility. Here’s the relevant documentation for using React’s classSet for class name manipulation.

Here’s an example of how the React classSet utility works from the documentation linked above.

1
2
3
4
5
6
7
8
9
10
render: function () {
     var cx = React.addons.classSet;
     var classes = cx({
         'message' : true ,
         'message-important' : this .props.isImportant,
         'message-read' : this .props.isRead
     });
     // same final string, but much cleaner
     return <div className={classes}>Great, I'll be there.</div>;
}

This is a simple example, but what about the case where the class is passed in via a prop as opposed to just being switched on or off by a boolean?

Second Try with React classSet

This try at adding classes via the React classSet utility allows us to add a class that is passed in via a prop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
render: function () {
     var classes = React.addons.classSet({
         'selected' : ( this .props.selected === this .props.className )
     });
 
     /*
      * Since the className changes from sidebar item to item,
      * we dynamically add this sidebar item's class as a key.
      */
     classes[ this .props.className ] = true ;
 
     return (
         <li className={ classes }>
             <a href={ this .props.href } onClick={ this .setLayoutFocus } >
                 <span className= "menu-link-text" >{ this .props.label }</span>
             </a>
         </li>
     );
}

Note that in this second try that we are dynamically appending our class that was passed in via the className prop to the classes object.

Then, when we call className={ classes } our string of classes is created.

Using classnames instead of classSet

React has deprecated React.addons.classSet and has suggested that developers use the classnames node module instead.

So, first things first, you’ll want to install classnames. You can usually do this in the root of your project, and you’ll use this command: npm install classnames.

Once you have installed classnames, you will then need to require it in your JSX file.

1
var classNames = require( 'classnames' );

Once we have required classnames, we should be able to simply swap outReact.addons.classSet with classNames. Even better though, sinceclassnames is so robust, we can just pass in this.props.className as an argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
render: function () {
     var classes = classNames( this .props.className, {
         'selected' : ( this .props.selected === this .props.className )
     } );
 
     return (
         <li className={ classes }>
             <a href={ this .props.href } onClick={ this .setLayoutFocus } >
                 <span className= "menu-link-text" >{ this .props.label }</span>
             </a>
         </li>
     );
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值