react native : What is the way to create an enum with 3 color types?

Viewed 420

In my example I try add a variable button_gradient_color with 3 parameters of color and it shows an error. The error is: Enum member must have initializer

What is the way to create an enum with 3 color types ?

export enum Colors {
 background_color = '#000060',
 button_gradient_color = '#4c669f', '#3b5998', '#192f6a'
};

this is the LinearGradient

<LinearGradient
            colors={ Colors.button_gradient_color1,Colors.button_gradient_color2,Colors.button_gradient_color3}

 >
2 Answers

Currently one variable initialized to multiple values is not allowed. You can either separate the color gradients into 3 different variables like below, if that helps you in your situation.

export enum Colors {
 background_color = '#000060',
 button_gradient_color1 = '#4c669f', 
 button_gradient_color2 = '#3b5998',
 button_gradient_color3 =  '#192f6a'
};

Playground

And you could refer like Colors.button_gradient_color1 perhaps.

While passing it to the LinearGradient component you can do is:

<LinearGradient colors={[ Colors.button_gradient_color1, Colors.button_gradient_color2  Colors.button_gradient_color3 ]} >

You can do it like this since the enum has already been declared

  <LinearGradient
    Colors={[
      Colors.button_gradient_color1, 
      Colors.button_gradient_color2,
      Colors.button_gradient_color3
    ]}
  >
Related