react-native 之style

react-native 不是通过css来实现app的styles,而是依赖于JavaScript。

一、声明一个styles:

var styles = StyleSheet.create({

base: {

width: 38,

height: 38,

},

background: {

backgroundColor: '#222222',

},

active:

{

borderWidth: 2,borderColor: '#00ff00',

},

});

查看其他属性名称,可以参考:http://facebook.github.io/react-native/docs/flexBox.html

二、使用styles

1、所有的核心组件都能应用styles

<Text style={styles.base} />

<View style={styles.background} />

2、也可以使用数组来限制一个组件

<View style={[styles.base,styles.background]} />

3、通常情况下,会在某个特定情况下加上styles

<View style={[styles.base,this.state.active && styles.active]} />

不鼓励在render中加入styles。

三、样式传递

为了让一个 call site 定制你的子组件的样式,你可以通过样式传递。使用View.propTypes.styleText.propTypes.style,以确保只有样式被传递了。

var List = React.createClass({
  propTypes: {
    style: View.propTypes.style,elementStyle: View.propTypes.style,},render: function() {
    return (
      <View style={this.props.style}>
        {elements.map((element) =>
          <View style={[styles.element,this.props.elementStyle]} />
        )}
      </View>
    );
  }
});
// ... in another file ...
<List style={styles.list} elementStyle={styles.listElement} />

相关文章

react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接...
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc ...