Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hardcoded strings/static contents in React/Redux app

I am optimizing a react/redux app and I wondered if there is the best practice to store and render hardcoded strings in such an app? Some of my components are using the same text and if there is a text change I will have to search all for all text occurrences. Would be better to have central location. I mean some static contents that y you do not want to put into state.

I would store all texts in a json object but I assume there must be a better way.

could anyone share his/her experience? any helpful tools or packages?

thank a lot for your answers!

like image 897
Petro Avatar asked Sep 01 '25 09:09

Petro


2 Answers

Nothing special here really regarding react or redux, you could just create a constants file...

export const VAR_NAME = "this is some text";
export const ANOTHER_VAR = "something else";

then when you want to use, simply:

import { ANOTHER_VAR } from 'path/to/constants/file';
like image 171
Dror Aharon Avatar answered Sep 03 '25 22:09

Dror Aharon


thank you for comments. In case, someone looks for nice solution this is - https://github.com/yahoo/react-intl

Requires some setup though but does what I wanted through making first steps towards internalization of the app.

like image 33
Petro Avatar answered Sep 03 '25 22:09

Petro