简体   繁体   English

React.js和Radium.js-使React样式本质上具有全局性,但需要用户输入吗?

[英]React.js & Radium.js - Making React styles global in nature, yet subject to user input?

I am just starting to learn Radium , so please overlook my ignorance. 我才刚刚开始学习Radium ,所以请忽略我的无知。 If there is anything great about CSS files , it is that you link to them once in your main APP component and forget them, they are cached and can be used in all your components simply. 如果CSS files有什么CSS files ,那就是您可以在主APP组件中将它们链接一次,而忘记它们了,它们将被缓存并可以在您的所有组件中简单使用。

But, you cannot connect any interactivity between them and any user input. 但是,您不能在它们和任何用户输入之间建立任何交互性。

Here is a simple snippet I played around with based on the sample provided on the Radium github : 这是我根据Radium github提供的示例使用的一个简单代码段:

import React, { Component, PropTypes } from 'react';
import Radium from 'radium';

const squareStyles = {
  both: {
    background: 'black',
    border: 'solid 1px white',
    float: 'left',
    height: 100,
    width: 100
  },
  one: {
    ':hover': {
      background: 'green'
    }
  },
  two: {
    ':hover': {
      background: 'red'
    }
  }
};

@Radium
export default class APP extends Component {

 ......................

  render() {
    // final result is yellow
    squareStyles.one[':hover'].background = 'yellow';
    return (
        <div>
          <div key="one" style={ [squareStyles.both, squareStyles.one] } />
          <div key="two" style={ [squareStyles.both, squareStyles.two] } />
          <div style={ { clear: 'both' } }/>
        </div>
    );
  }
}

Even though the squareStyles.one[':hover'].background was preset to green, user input made it yellow instead. 即使squareStyles.one [':hover']。background预设为绿色,但用户输入却改为黄色。 Great! 大!

But what if I need squareStyles in several components? 但是,如果我需要几个组件中的squareStyles怎么办? I do not want repetitive code defining squareStyles in each of them. 我不想在每个代码中都定义正方形样式的重复代码。

Question: 题:

  1. With Radium, or any 3rd party add-on, is there a way I can have a styles.js file global in nature much like css? 使用Radium或任何第三方插件,是否有办法像CSS那样使全局的styles.js文件具有全局性?

  2. If not, who would even consider inline styles when it would just lead to repetitive code that one of the benefits of css takes care of? 如果不是这样,当css的优点之一引起关注的重复代码出现时,谁还会考虑内联样式?

Thanks 谢谢


Update: 更新:

  1. I put square styles in a js file encapsulated under module.exports. 我将方形样式放入封装在module.exports下的js文件中。
  2. I added const styles = require('../styles/styles'); 我添加了const styles = require('../styles/styles'); in lieu of the local squareStyles object. 代替本地squareStyles对象。
  3. I prefaced all objects of 'squareStyles.' 我为“ squareStyles”的所有对象添加了前缀。 to 'styles.squareStyles'. 到'styles.squareStyles'。

Everything works, so ... 一切正常,所以...

I assume I will have to add that required file to every component. 我假设我将必须将所需的文件添加到每个组件。 Yes? 是?

  1. Will the file be cached? 会缓存文件吗?

  2. If I convert all my css files this way to take advantage of user interactivity, will my app become slow? 如果我以此方式转换所有css文件以利用用户交互性,我的应用程序会变慢吗?

  3. Am I even going about this correctly? 我什至能正确解决这个问题吗?

Again thanks. 再次感谢。

  1. With Radium, or any 3rd party add-on, is there a way I can have a styles.js file global in nature much like css? 使用Radium或任何第三方插件,是否有办法像CSS那样使全局的styles.js文件具有全局性?

Yeah sure you can use Webpack to automatically make certain things global, however you don't want to do this. 是的,请确保您可以使用Webpack自动使某些内容全局化,但是您不想这样做。 The idea of requiring / important is to not only explicitly manage dependencies, but also reduce the footprint of your site. 要求/重要的想法不仅是显式管理依赖关系,而且还可以减少您的网站占用空间。 If you eventually want to get into things like code splitting and loading only the necessary parts of an application up front, you'll want to require things manually. 如果您最终想要进行诸如代码拆分和仅预先加载应用程序必要部分之类的工作,则需要手动进行操作。

  1. If not, who would even consider inline styles when it would just lead to repetitive code that one of the benefits of css takes care of? 如果不是这样,当css的优点之一引起关注的重复代码出现时,谁还会考虑内联样式?

Inline code CAN lead to repetitive code, and CSS can also leave to repetitive code. 内联代码可以导致重复代码,而CSS也可以保留重复代码。 One of the things CSS allowed developers to do, was to be lazy and write classes and not have to worry about if there was a class that already addressed the majority of the visual representation they wanted. CSS允许开发人员做的一件事是懒惰并编写类,而不必担心是否有一个类已经解决了他们想要的大多数可视表示形式。 It's the age old idea that you can use the best language and technology stack and still write bad code. 古老的想法是,您可以使用最佳的语言和技术堆栈,但仍可以编写错误的代码。

The thing about React, is that it allows you to create reusable components. 关于React的事情是,它允许您创建可重用的组件。 You write a couple of reusable component, you import them in another component, then you're only having to write the inline styles once, same as css. 您编写了几个可重用的组件,将它们导入另一个组件,然后只需要编写一次内联样式即可,就像CSS一样。 You can argue that importing components is more tedious, however there are plenty of problems with css that you'll be getting around by taking this approach. 您可以争辩说,导入组件比较繁琐,但是采用此方法将可以解决css的许多问题。 See here: https://speakerdeck.com/vjeux/react-css-in-js 看到这里: https : //speakerdeck.com/vjeux/react-css-in-js

  1. Will the file be cached? 会缓存文件吗?

No. Browsers are built to support CSS and cache them. 不会。浏览器的构建是为了支持CSS并缓存它们。 But you have to ask yourself: is the different negligible? 但是您必须问自己:差异可以忽略不计吗? For instance more than ever, people are more concerned about users hitting your site for the first, which is where the bounce rate is the highest. 例如,人们比以往任何时候都更加关注用户首次访问您的网站,这是跳出率最高的地方。 You can argue for a cached css file, however if this css file represents the entirety of your application, it will most certainly take longer than inline styles from a couple of react components. 您可以主张使用缓存的css文件,但是,如果此css文件代表您的整个应用程序,则肯定比两个反应组件的内联样式花费的时间更长。 Moreover, mounting additional components with additional css may be trivial enough load wise that it wouldn't matter. 而且,用额外的CSS来安装额外的组件可能对负载无关紧要,这无关紧要。 I'm still measuring this stuff too but what's important to note is that you should think outside the box in terms of what css has preached to developers all these years. 我也仍然在评估这些东西,但是需要注意的重要一点是,您应该考虑这些年来CSS向开发人员宣讲的内容。 There may be a better solution... try it out for yourself performance wise and report back! 可能有更好的解决方案...尝试自己的性能,然后报告!

  1. If I convert all my css files this way to take advantage of user interactivity, will my app become slow? 如果我以此方式转换所有css文件以利用用户交互性,我的应用程序会变慢吗?

https://github.com/FormidableLabs/radium/issues/58 https://github.com/FormidableLabs/radium/issues/58

Word on the street is that the performance hit you would get is negligible. 大街上有消息说,您获得的性能影响可以忽略不计。 Again, put together a small test and see if the performance works for you. 再次,进行一次小型测试,看看性能是否适合您。

  1. Am I even going about this correctly? 我什至能正确解决这个问题吗?

A lot of new methods take time for them to catch adoption with developers, and when they do usually you'll see different patterns come out. 许多新方法使他们需要时间来吸引开发人员的采用,而当他们这样做时,通常会看到不同的模式。 The pattern you're using is definitely an option. 您使用的模式绝对是一个选择。 You may see that option refined, changed or even scrapped (similar to the path of flux if you've been paying attention that project and all it's refinements / spinoffs). 您可能会看到该选项经过完善,更改甚至报废(类似于不断变化的路径,如果您一直在关注该项目及其所有改进/衍生产品)。 I'd say to encapsulate the css into as many reusable components as possible, then use a file like yours as a way to reuse chunks of style that wouldn't necessarily be a component (for instance a piece of code that adds text shadow and box shadow to an element / component), and then lastly leverage one off inline styles for things that aren't patterns or won't be used. 我想说的是,将css封装到尽可能多的可重用组件中,然后使用像您这样的文件来重用不一定是组件的样式块(例如,一段添加文本阴影和框阴影添加到元素/组件),然后最后将非样式或非样式的东西利用一种内联样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM