我可以在React Native中创建动态样式吗?

编程入门 行业动态 更新时间:2024-10-25 15:29:12
本文介绍了我可以在React Native中创建动态样式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有一个包含这样的渲染的组件:

Say I have a component with a render like this:

<View style={jewelStyle}></View>

其中jewelStyle =

Where jewelStyle =

{ borderRadius: 10, backgroundColor: '#FFEFCC', width: 20, height: 20, },

如何使背景颜色动态和随机分配?我尝试过

How could I make the background colour dynamic and randomly assigned? I've tried

{ borderRadius: 10, backgroundColor: getRandomColor(), width: 20, height: 20, },

任何提示?

推荐答案

我通常会做以下行:

<View style={jewelStyle()} />

...

jewelStyle = function(options) { return { borderRadius: 10, background: randomColor(), } }

每次渲染视图时,将使用与其关联的随机颜色来实例化新的样式对象。当然,这意味着颜色将在每次组件被重新渲染时改变,这可能不是你想要的。您可以这样做:

Every time View is rendered, a new style object will be instantiated with a random color associated with it. Of course, this means that the colors will change every time the component is re-rendered, which is perhaps not what you want. Instead, you could do something like this:

var myColor = randomColor() <View style={jewelStyle(myColor)} />

...

jewelStyle = function(myColor) { return { borderRadius: 10, background: myColor, } }

更多推荐

我可以在React Native中创建动态样式吗?

本文发布于:2023-11-06 14:38:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563967.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式   动态   React   Native

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!