Inline CSS styles in React: how to implement a:hover?

前端 未结 18 892
星月不相逢
星月不相逢 2020-11-28 21:51

I quite like the inline CSS pattern in React and decided to use it.

However, you can\'t use the :hover and similar selectors. So what\'s the best way to

18条回答
  •  孤独总比滥情好
    2020-11-28 22:06

    Made Style It -- in part -- because of this reason (others being disagreements with implementation of other libs / syntax and inline stylings lack of support for prefixing property values). Believe we should be able to simply write CSS in JavaScript and have fully self contained components HTML-CSS-JS. With ES5 / ES6 template strings we now can and it can be pretty too! :)

    npm install style-it --save

    Functional Syntax (JSFIDDLE)

    import React from 'react';
    import Style from 'style-it';
    
    class Intro extends React.Component {
      render() {
        return Style.it(`
          .intro:hover {
            color: red;
          }
        `,
          

    CSS-in-JS made simple -- just Style It.

    ); } } export default Intro;

    JSX Syntax (JSFIDDLE)

    import React from 'react';
    import Style from 'style-it';
    
    class Intro extends React.Component {
      render() {
        return (
          
        );
      }
    }
    
    export default Intro;
    

提交回复
热议问题