介紹原始寫法 & 及其改進寫法一css
還有比較流行的 styled-components在RN中的使用 & 及其改進寫法二前端
/** * 原寫法 */
const styles1 = StyleSheet.create({
item1:{
width:100,
height:200,
backgroundColor:'#66CCFF',
},
item2:{
width:100,
height:200,
backgroundColor:'#66CCFF',
},
item3:{
width:50,
height:100,
top:50,
left:25,
backgroundColor:'#66CCFF',
}
});
console.log('styles1 :',styles1);
複製代碼
原寫法的缺點在於變量很差引入,很差體現樣式間的關係,沒有計算表達式等……node
看StyleSheet.create的代碼,就是直接返回一個對象react
//在react native 0.44版本中
var StyleSheet = {
create: function(styles) {
return styles;
},
複製代碼
那就能夠不限於StyleSheet.create的寫法,能夠比較自由的返回一個對象了,下面給出一個個人簡單例子:git
/** * 換一種寫法,簡單引入了變量表達式 * 雖然仍是沒有像iOS中 view.center / autolayout之類的寫法方便 * @returns {{}} */
function styles2Creator() {
let s = {};
let itemWidth = 100;
let itemHeight = 200;
//引用常量
s.item1 = {
width:itemWidth,
height:itemHeight,
backgroundColor:'#66CCFF',
};
//引用其餘樣式的值
s.item2 = {
width:s.item1.width,
height:itemHeight,
backgroundColor:`${s.item1.backgroundColor}`,
};
//計算表達式
s.item3 = {
width: s.item2.width / 2,
height: s.item2.height / 2,
top:s.item2.height / 4,
left:s.item2.width / 4,
backgroundColor:s.item1.backgroundColor,
};
//樣式的繼承
s.item4 = {
...s.item3,
backgroundColor:'#FF00CC',
};
//帶參數
s.item5 = (top) => {
return {
...s.item3,
marginTop:top,
};
};
//帶參數 + 缺省值
s.item6 = (top) => {
return {
...s.item3,
marginTop:top ? top : 10,
};
}
return s;
}
const style2 = styles2Creator();
//const style2 = StyleSheet.create(styles2Creator());
console.log('style2 :',style2);
複製代碼
運行一下能夠看到 log出來的style2和style1的屬性。github
號稱React 中的 CSS 最佳實踐,使用行內樣式,支持CSS。該第三方也也可用於RN。npm
react-native init
了個RN工程 寫了個簡單的例子:react-native
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image
} from 'react-native';
//須要 `npm install --save styled-components`
import styled from "styled-components/native";
//這裏開始styled-components 式的樣式:
//styled-components的格式爲: const [自定義變量名] = styled.[相應的系統組件]`css表達式`;
//普通寫法 (不大寫開頭會報錯……
const ListContainerView = styled.View` width:360; height:280; background-color: #F0F2F5; `;
//擴展 (backgroundColor 和 background-color均可以
const ItemContainerView = ListContainerView.extend` backgroundColor: #66CCFF; flexDirection:row; `;
//帶參數
const LeftImageContainerView = styled.View` height:${props => props.primary ? 280 : 180};; width:180; background-color: #77BB00; `;
//而後發現一個尷尬的事就是不知道怎麼擴展自定義組件 好比JDImage
//帶計算表達式
const LeftImageHeight = 280 - 10 *2;
const LeftImage = styled.Image` margin-top:10; margin-left:10; width:${180 - 10 *2}; height:${LeftImageHeight}; background-color: #FFFFFF; `;
//想要獲取另外一個組件樣式LeftImage的高度 不能直接使用 ${LeftImage.height}
//由於 LeftImage返回的是 ƒ StyledNativeComponent()方法……
const RightContainerView = styled.View` width:160; height:${LeftImageHeight}; background-color: #FF00CC; `;
export default class MyApp extends Component {
render() {
return (
<ItemContainerView > <LeftImageContainerView primary> {console.log('LeftImageContainerView.style:',LeftImageContainerView)} <LeftImage source={{uri:'http://static.runoob.com/images/demo/demo2.jpg'}}/> </LeftImageContainerView> <RightContainerView></RightContainerView> </ItemContainerView> ); } } AppRegistry.registerComponent('MyApp', () => MyApp); 複製代碼
優勢:sass
缺點和原始寫法的差很少,還對自己不是前端開發的人來講帶來額外的學習成本…… 因此仍是不推薦……less
參考連接:
簡單來說 styled-components 就是生成一個帶樣式的組件,徹底能夠吸取這種寫法 本身改進RN中 ,而不使用styled-components這個庫
結合 寫法一 和 styled-components的想法,給出一個簡單例子:
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image
} from 'react-native';
//寫法二
function stylesCreator() {
let s = {};
let width = 360.0, height = 280.0;
s.ListContainerView = {
width: width,
height: height,
backgroundColor: '#F0F2F5',
};
s.ItemContainerView = {
width: width,
height: height,
backgroundColor: '#66CCFF',
flexDirection:'row',
};
console.log('s.ItemContainerView :',s.ItemContainerView);
s.LeftImageContainerView = {
height:height,
width:width / 2,
backgroundColor: '#77BB00',
};
s.LeftImage = {
marginTop:10,
marginLeft:10,
width: 180 - 10 *2,
height: s.LeftImageContainerView.height - 10*2,
backgroundColor: `#FFFFFF`,
};
s.RightContainerView = {
width: width / 2,
height: s.LeftImage.height,
backgroundColor: '#FF00CC',
};
return s;
}
const styles = stylesCreator();
//const styles = StyleSheet.create(stylesCreator());
//而後再結合 styled-components:
// 模擬 styled-components API
const styled = (Component, styler) => (props) => {
const style = typeof styler === 'function' ? styler(props) : styler;
return <Component {...props} style={[ style, props.style ]} />
}
// styled components
//一樣能夠完成組件(帶樣式)的繼承 const RightContainerView = styled(LeftImageContainerView,styles.RightContainerView);
const ListContainerView = styled(View,styles.ListContainerView);
const ItemContainerView = styled(View,styles.ItemContainerView);
const LeftImageContainerView = styled(View,styles.LeftImageContainerView);
const LeftImage = styled(Image,styles.LeftImage);
const RightContainerView = styled(View,styles.RightContainerView);
export default class MyApp extends Component {
render() {
return (
<ItemContainerView >
<LeftImageContainerView primary>
{console.log('LeftImageContainerView.style:',LeftImageContainerView)}
<LeftImage source={{uri:'http://static.runoob.com/images/demo/demo2.jpg'}}/>
</LeftImageContainerView>
<RightContainerView></RightContainerView>
</ItemContainerView>
);
}
}
AppRegistry.registerComponent('MyApp', () => MyApp);
複製代碼
emmm ,無需引入第三方庫 感受好多了。缺點固然是不支持原CSS寫法。
在0.45版本中運行改進寫法一時,你可能看到style2在控制檯的輸出相似爲:
//const style2 = StyleSheet.create(styles2Creator());
style2 :
{item1: 12, item2: 13, item3: 14, item4: 15, item5: 16, …}
item1:12
item2:13
item3:14
item4:15
item5:16
item6:17
__proto__:Object
複製代碼
這是怎麼肥事!個人item對象怎麼變成數字了!
別急,在0.45版本後StyleSheet代碼有所改變(其實我沒看具體哪一個小版本改的 _(:зゝ∠)_
), StyleSheet.create改爲:
//react native : 0.45.1
create<S: Styles>(obj: S): StyleSheet<S> {
const result: StyleSheet<S> = {};
for (var key in obj) {
StyleSheetValidation.validateStyle(key, obj);
result[key] = ReactNativePropRegistry.register(obj[key]);
}
return result;
},
複製代碼
//react native0.45.1 ReactNativePropRegistry.js
var objects = {};
var uniqueID = 1;
var emptyObject = {};
class ReactNativePropRegistry {
static register(object: Object): number {
var id = ++uniqueID;
if (__DEV__) {
Object.freeze(object);
}
objects[id] = object;
return id;
}
//多的就不看了……內容很少各位有興趣本身看
複製代碼
經過看ReactNativePropRegistry代碼,StyleSheet將樣式對象儲存在objects
中,並返回uniqueID
好比取回原來的item,就能夠這樣作:
import ReactNativePropRegistry from '../node_modules/react-native/Libraries/Renderer/src/renderers/native/ReactNativePropRegistry';
console.log("ReactNativePropRegistry.getByID(12): ",ReactNativePropRegistry.getByID(12));
console.log("ReactNativePropRegistry.getByID(style2. item1): ",ReactNativePropRegistry.getByID(style2. item1));
複製代碼
就能夠經過 ReactNativePropRegistry.getByID就能夠取得樣式對象了。這可能對於以前的改進寫法形成了一點小麻煩,不過還能夠用~
還能夠用 StyleSheet.flatten(style )取得樣式對象;
其餘參考閱讀: