- ReactJS by Example:Building Modern Web Applications with React
- Vipul A M Prathamesh Sonpatki
- 370字
- 2021-07-09 19:37:00
Styles in JSX
"Mike, all of the things that we did today are very cool. When do we start adding styles? How can I make this page look pretty? Right now it's a bit dull." Shawn asked.
"Ah, right. Let's do that. React allows us to pass styles to the components the same way props can be passed. For example, we want our headings to be of the floral white color and maybe we want to change the font size. We will represent it in a typical CSS way as follows:"
background-color: 'FloralWhite', font-size: '19px';
"We can represent this as a JavaScript object in the CamelCase fashion."
var headingStyle = { backgroundColor: 'FloralWhite', fontSize: '19px' };
Then, we can use it in each Heading component as a JavaScript object."
RecentChangesTable.Heading = React.createClass({ render: function() { var headingStyle = { backgroundColor: 'FloralWhite', fontSize: '19px' }; return(<th style={headingStyle}>{this.props.heading}</th>); } });
"Similarly, let's change the rows to have their own styles."
RecentChangesTable.Row = React.createClass({ render: function() { var trStyle = { backgroundColor: 'aliceblue' }; return <tr style={trStyle}> <td>{this.props.changeSet.when}</td> <td>{this.props.changeSet.who}</td> <td>{this.props.changeSet.description}</td> </tr>; } });
"We now have some shiny new headings and rows sprinkled by CSS styling" Mike added.

"Nice. The name of the attribute to pass these styles must be 'style', right?" Shawn asked.
"Yes. Also the keys of this style object need to be CamelCased, for example, backgroundColor
, backgroundImage
, fontSize
, and so on."
"Mike, I understood the inline styling, however, how to add the CSS classes?"
"Ah right. We can pass the class name as an attribute to the DOM tags. Let's extract the styling to a new recentChangesTable
CSS class."
// css recentChangesTable { background-color: 'FloralWhite', font-size: '19px' }
"Now, to apply this class to our component, all we need to do is pass it to the component using the expalined className
prop."
render: function(){ return <table className = 'recentChangesTable'> <Headings headings = {this.props.headings} /> <Rows changeSets = {this.props.changeSets} /> </table>; } });
"As we have seen previously, React makes use of the CamelCase attributes. Here, the className
prop will be converted to a normal class attribute when React renders the actual HTML."
<table class = 'recentChangesTable'> … </table>
"We can also pass multiple classes to the className
prop."
<table className = 'recentChangesTable userHeadings'>
"That's it! We can simply start using the styles freely in our components. This makes it trivial to integrate React with existing CSS styles."
- 趣學(xué)Python算法100例
- 基于免疫進(jìn)化的算法及應(yīng)用研究
- The Computer Vision Workshop
- 零基礎(chǔ)入門(mén)學(xué)習(xí)Python
- Serverless架構(gòu)
- 編寫(xiě)高質(zhì)量代碼:改善Objective-C程序的61個(gè)建議
- OpenCV with Python By Example
- 零代碼實(shí)戰(zhàn):企業(yè)級(jí)應(yīng)用搭建與案例詳解
- Learning Python Data Visualization
- 現(xiàn)代CPU性能分析與優(yōu)化
- 實(shí)戰(zhàn)Python網(wǎng)絡(luò)爬蟲(chóng)
- Getting Started with RethinkDB
- Unity 3D UI Essentials
- C語(yǔ)言王者歸來(lái)
- Learning Dynamics NAV Patterns