Ilja
Ilja

Reputation: 46479

Is there any performance drawback in using functions inside StyleSheet?

To simplify code and pass props to my styles I came up with this sollution

const styles = StyleSheet.create({
  someNormalStyle: {
    backgroundColor: 'red',
  },
  dynamicStyle(color) {
   return {
    backgroundColor: color
   }
  }
})

which I would then use normally in my components i.e.

<View style={style.someNormalStyle} />
<View style={style.dynamicStyle('blue')} />

This seems to work, I just wanted to verify if there is any performance hit from doing this?

Upvotes: 0

Views: 30

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138267

Did you actually race the horses ?

If you do, you'll probably notice that one function call won't cost you that much, and the engine might even inline it.

"To simplify code" ... yup, thats the point here.

Upvotes: 1

Related Questions