1. React中的样式
React并没有像Vue那样提供特定的区域给我们编写CSS代码
所以你会发现在React代码中, CSS样式的写法千奇百怪
2. 内联样式
class App extends React.Component{
constructor(props){
super(props);
this.state = {
color: 'red'
}
}
render(){
return (
<div>
<p style={{fontSize:'50px', color: this.state.color}}>我是段落1</p>
<p style={{fontSize:'50px', color:'green'}}>我是段落2</p>
<button onClick={()=>{this.btnClick()}}>按钮</button>
</div>
);
}
btnClick(){
this.setState({
color: 'blue'
})
}
}
export default App;
3. 外链样式
将CSS代码写到一个单独的CSS文件中, 在使用的时候导入进来
//Home.tsx文件
import React from 'react';
import './Home.css'
class Home extends React.Component{
render() {
return (
<div id={'home'}>
<p>我是home段落</p>
<a href={'www.it666.com'}>我是home超链接</a>
</div>
)
}
}
export default Home;
/*Home.css文件*/
#home p{
font-size: 50px;
color: red;
}
#home a{
color: yellow;
}
4.Css Module

const str = `my name is ${name}, my age is ${age}`;
console.log(str); // my name is yiya_xiaoshan, my age is 18
除此之外,react中还有一些ES6中没有的特性
在JS中除了可以通过()来调用函数以外,,其实我们还可以通过模板字符串来调用函数
function test(...args) {
console.log(args);
}
test(1, 3, 5); // [ 1, 3, 5 ]
test`1, 3, 5`; // [ [ '1, 3, 5' ] ]
通过模板字符串调用函数规律:
test`1, 3, 5, ${name}, ${age}`;
// [ [ '1, 3, 5, ', ', ', '' ], 'yiya_xiaoshan', 18 ]
4. CSS-in-JS
1. 使用CSS-in-JS的原因
样式嵌套、函数定义、逻辑复用、动态修改状态等特性, 也就是说, 从某种层面上, 提供了比过去Less/Scss更为强大的功能,所以Css-in-JS, 在React中也是一种比较推荐的方式2.styled-components使用
xxx:xxx
import React from 'react';
import styled from 'styled-components';
// 注意点:
// 默认情况下在webstorm中编写styled-components的代码是没有任何的代码提示的
// 如果想有代码提示, 那么必须给webstorm安装一个插件
const StyleDiv = styled.div`
p{
font-size: 50px;
color: red;
}
a{
font-size: 25px;
color: green;
}
`;
class Home extends React.Component{
render() {
return (
<StyleDiv>
<p>我是home段落</p>
<a href={'www.it666.com'}>我是home超链接</a>
</StyleDiv>
)
}
}
export default Home;
5. styled-components
5.1 styled-components特性之- props(回调函数)和- attrs
import React from 'react';
//1.导入style-components库
import styled from 'styled-components';
//2.通过回调函数调用props里头的数据
// 通过回调函数调用attrs设置样式
const StyleDiv = styled.div`
p{
font-size: 50px;
color: ${props => props.color};
}
`;
const StyleInput = styled.input.attrs({
type:'password'
})``
class Home extends React.Component{
constructor(props){
super(props);
this.state = {
color: 'red'
}
}
render() {
return (
<StyleDiv color={this.state.color}>
<p>我是home段落</p>
<a href={'www.it666.com'}>我是home超链接</a>
<button onClick={()=>{this.btnClick()}}>按钮</button>
<StyleInput></StyleInput>
</StyleDiv>
)
}
btnClick(){
this.setState({
color: 'green'
})
}
}
export default Home;
5.2 如何通过style-components统一设置样式——ThemeProvider
// 在App.js引入ThemeProvider
import { ThemeProvider } from 'styled-components';
<!--通过需要应用样式的组件用ThemeProvider包裹-->
<ThemeProvider theme={{size:'100px',color:'green'}}>
<Header/>
<Header name={"sjl"} age={20}></Header>
</ThemeProvider>
高阶组件会自动将其ThemeProvider提供的数据保存到子代的props
import React from 'react'
import ReactTypes from 'prop-types'
import styled from 'styled-components'
const StyleDiv1 = styled.div`
p{
color:${props=>props.theme.color};
font-size:${props=>props.theme.size}
}
`
function Header(props) {
console.log(props)
return (
<div>
<div className={'header'}>我是头部</div>
<StyleDiv1>我的世界</StyleDiv1>
</div>
)
}
export default Header;
5.3 style-components的继承关系
import React from 'react';
import styled from 'styled-components'
/* const StyleDiv1 = styled.div`
font-size: 100px;
color: red;
background: blue;
`;
const StyleDiv2 = styled.div`
font-size: 100px;
color: green;
background: blue;
`; */
const BaseDiv = styled.div`
font-size: 100px;
background: blue;
`;
const StyleDiv1 = styled(BaseDiv)`
color: red;
`;
const StyleDiv2 = styled(BaseDiv)`
color: green;
`;
class App extends React.Component{
render(){
return (
<div>
<StyleDiv1>我是div1</StyleDiv1>
<StyleDiv2>我是div2</StyleDiv2>
</div>
);
}
}
export default App;
我在MiniTest::Spec和Capybara中使用以下规范:find_field('Email').must_have_css('[autofocus]')检查名为“电子邮件”的字段是否具有autofocus属性。doc说如下:has_css?(path,options={})ChecksifagivenCSSselectorisonthepageorcurrentnode.据我了解,字段“Email”是一个节点,因此调用must_have_css绝对有效!我做错了什么? 最佳答案 通过JonasNicklas得到了答案:No
我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes
我有一个将某些事件写入队列的Rails3应用。现在我想在服务器上创建一个服务,每x秒轮询一次队列,并按计划执行其他任务。除了创建ruby脚本并通过cron作业运行它之外,还有其他稳定的替代方案吗? 最佳答案 尽管启动基于Rails的持久任务是一种选择,但您可能希望查看更有序的系统,例如delayed_job或Starling管理您的工作量。我建议不要在cron中运行某些东西,因为启动整个Rails堆栈的开销可能很大。每隔几秒运行一次它是不切实际的,因为Rails上的启动时间通常为5-15秒,具体取决于您的硬件。不过,每天这样做几
我开始了一个新的Rails3.2.5项目,Assets管道不再工作了。CSS和Javascript文件不再编译。这是尝试生成Assets时日志的输出:StartedGET"/assets/application.css?body=1"for127.0.0.1at2012-06-1623:59:11-0700Servedasset/application.css-200OK(0ms)[2012-06-1623:59:11]ERRORNoMethodError:undefinedmethod`each'fornil:NilClass/Users/greg/.rbenv/versions/1
rails新手。只是想了解\assests目录中的这两个文件。例如,application.js文件有如下行://=requirejquery//=requirejquery_ujs//=require_tree.我理解require_tree。只是将所有JS文件添加到当前目录中。根据上下文,我可以看出requirejquery添加了jQuery库。但是它从哪里得到这些jQuery库呢?我没有在我的Assets文件夹中看到任何jquery.js文件——或者直接在我的整个应用程序中没有看到任何jquery.js文件?同样,我正在按照一些说明安装TwitterBootstrap(http:
我正在尝试消除使用Bootstrap3的Rails4元素中的glyphicon错误。我没有使用任何Bootstrapgem将其添加到Assets管道中。我手动将bootstrap.css和bootstrap.js添加到各自的app/assets目录下,分别添加到application.css和application.js什么的我现在在网络浏览器的控制台中看到以下内容:GEThttp://localhost:3000/fonts/glyphicons-halflings-regular.woff404(NotFound)localhost/:1GEThttp://localhost:30
我有一个使用twitterbootstrap和sass的Rails元素。scss文件结构化到文件夹中,所以我有更好的概述。现在我想为包含我的颜色等的全局变量定义一个文件,并将这些值传递给其他文件,这样我就有更少的冗余代码。虽然所有代码都已正确导入和应用,变量不起作用。这是当前的设置:样式表/application.css.scss/**=require_self*=require_tree*//*stylesheets/||–base/||–_reset.scss#Reset/normalize||–_typography.scss#Typographyrules||–componen
有没有一种方法可以在jekyll站点中包含自定义css标签,同时将markdown用于入口文件?例如,当我想突出显示某个段落时? 最佳答案 Markdown和YAMLFrontMatter都内置了这个。但你可以自己制作。比如说,您有foo.css想要包含在某些帖子中。在_posts/2013-02-03-higligting-foo.markdown中:---css:footitle:"DrupalImagecachesecurityvulnarabilitywithDDOSattackexplained"tags:[drupal,
我在一台Windows764位机器上使用Sass和Ruby(最新版本),我正在我的家庭服务器上处理一个共享文件夹。(但是,我不得不承认问题本身也出现在服务器上,因为我试图安装Ruby并直接-watch服务器上的文件)。问题如下:如果我第一次保存,检测到变化,我的style.css被直接覆盖。之后,我总是需要保存多达7次才能覆盖style.css。每次都会检测到更改,但不会编译任何内容。这是一个屏幕:>>>Sassiswatchingforchanges.PressCtrl-Ctostop.overwritestyle.css>>>Changedetectedto:E:/Websites
是否有一个SASS扩展可以采用SASS样式表,找到中性属性(例如border-radius)并为其输出所有特定于供应商的属性(例如-webkit-border-radius等)自动?我真的不想手动创建所有混入,也不想手动编写代码。我确定一定有这样的扩展名,但我找不到它。帮忙? 最佳答案 有一个非常好的gem可以满足您的需求。它叫做Bourbon它不会用特定于供应商的css替换您的css,因为它可以像SASS一样工作。它基本上是一个正确生成跨浏览器css的mixin集合。 关于ruby-用