Say I have a component with a render like this:
<View style={jewelStyle}></View>
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,
},
But this makes all instances of View have the same colour, I want each one to be unique.
Any tips?
Had some issue syntactically. This worked for me
The easiest is mine:
I know there are several answers, but i think the best and most simple is using a state "To change" is the state purpose.
}
You'll want something like this:
In this example I take the rows array, containing the data for the rows in the component, and map it into an array of Text components. I use inline styles to call the
getRandomColor
function every time I create a new Text component.The issue with your code is that you define the style once and therefore getRandomColor only gets called once - when you define the style.
You can bind state value directly to style object. Here is an example:
In case someone needs to apply conditions