Sometimes, we want to create constants file with React.
In this article, we’ll look at how to create constants file with React.
How to create constants file with React?
To create constants file with React, we can create a JavaScript file with variables exported.
For instance, we write
fileWithConstants.js
export const ACTION_INVALID = "This action is invalid!";
export const CONSTANT_NUMBER_1 = "hello I am a constant";
export const CONSTANT_NUMBER_2 = "hello I am also a constant";
to export the ACTION_INVALID, CONSTANT_NUMBER_1 and CONSTANT_NUMBER_2 constants in a file.
Then we can use them by writing
import * as consts from "path/to/fileWithConstants";
const errorMsg = consts.ACTION_INVALID;
to import all the contents of fileWithConstants.js that are export and reference ACTION_INVALID with consts.ACTION_INVALID.
How to create constants file with React?
To create constants file with React, we can create a JavaScript file with variables exported.