React Native 样式与平台 API 全解析
1. React Native 样式相关
1.1 传递样式作为属性
在 React Native 中,我们可以将样式作为属性传递给组件。 View.propTypes.style 这个 propType 能确保只有有效的样式被作为属性传递。借助这种模式,我们可以创建可扩展的组件,这些组件能被其父组件更有效地控制和设置样式。
下面是一个示例,展示了组件如何通过属性接收样式对象:
'use strict';
var React = require('react-native');
var {
View,
Text
} = React;
var CustomizableText = React.createClass({
propTypes: {
style: Text.propTypes.Style
},
getDefaultProps: function() {
return {
style: {}
};
},
render: function() {
return (
<Text style={[myStyles.text, this.props.style]}>
Hello, world
</Text>
);
}
});
通过将 this.props.style 添加到样式数组的末尾,我们可
超级会员免费看
订阅专栏 解锁全文
30

被折叠的 条评论
为什么被折叠?



