Setting Complex react inline styles such as hover, active on react components such as button

CSS in JS (with pseudo classes & MQ support)

There are lots of libs to write CSS with React that supports pseudo classes but all, if not all of them requires you to inline or write your CSS in JS Which I highly recommend

CSS Modules (Write your CSS as normal, but in a better way)

There is also CSS modules which if you are already using Webpack should be simple to set it up, which let you import/require your CSS as an object use it your component like so:

import styles from './Button.css'

const MyButton = ({children, onClick, type="primary", ...rest }) =>
(
    <button
        onClick   = {onClick}
        className = {styles.primary}
        {...rest}
    >
        {children}
    </button>
);

Decoupling your components

I’d also add that you shouldn’t pass classes to the <Button /> and deal with the conditions inside the component itself. For example using classnames lib you can do something like the following.

import classnames from 'classnames'

const MyButton = ({children, onClick, type="primary", ...rest }) =>
(
    <button
        onClick   = {onClick}
        {/* 
          depends on the type prop, you will get the relevent button 
          so no need for consumers to care about the internals of the component
        */}
        className = {classnames('.btn', { [`btn-${type}`]: true })}
        {...rest}
    >
        {children}
    </button>
);

You can even combine CSS Modules & classnames lib to create some powerful combinations.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)