9951 explained code solutions for 126 technologies


reactjsHow can I use enums in ReactJS?


Enums can be used in ReactJS to define a set of constants that represent a specific value. For example, you can use an enum to define a set of colors:

enum Colors {
  Red = '#FF0000',
  Green = '#00FF00',
  Blue = '#0000FF',
}

You can then use the enum values in your React components, such as in a style attribute:

<div style={{ color: Colors.Red }}>This text will be red</div>

The output will be:

This text will be red

Code explanation

  1. enum Colors { ... }: This defines the enum and its values.
  2. Colors.Red: This is how you access the values of the enum.
  3. style={{ color: Colors.Red }}: This is how you use the enum values in a React component.

Helpful links

Edit this code on GitHub