npm:https://www.npmjs.com/package/style-it
github:https://github.com/buildbreakdo/style-it

函数语法

(1)使用

  1. import React from 'react';
  2. import Style from 'style-it';
  3. class Intro extends React.Component {
  4. render() {
  5. return Style.it(`
  6. .intro {
  7. font-size: 40px;
  8. }
  9. `,
  10. <p className="intro">CSS-in-JS made simple -- just Style It.</p>
  11. );
  12. }
  13. }
  14. export default Intro;

(2)输出

  1. <p class="intro _scoped-1">
  2. <style type="text/css">
  3. ._scoped-1.intro {
  4. font-size: 40px;
  5. }
  6. </style>
  7. CSS-in-JS made simple -- just Style It.
  8. </p>

JSX 语法

(1)使用

  1. import React from 'react';
  2. import Style from 'style-it';
  3. class Intro extends React.Component {
  4. render() {
  5. return (
  6. <Style>
  7. {`
  8. .intro {
  9. font-size: 40px;
  10. }
  11. `}
  12. <p className="intro">CSS-in-JS made simple -- just Style It.</p>
  13. </Style>
  14. );
  15. }
  16. }
  17. export default Intro;

(2)输出

  1. <p class="intro _scoped-1">
  2. <style type="text/css">
  3. ._scoped-1.intro {
  4. font-size: 40px;
  5. }
  6. </style>
  7. CSS-in-JS made simple -- just Style It.
  8. </p>

使用实例

(1)配合canvas生成自定义水印背景