Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach for managing strings in react native

I am new in react native. I've been dealing with this big project, that contains too many strings that can be reused many places in the project. So I created a strings.js file , as in android's strings.xml, to store all reusable strings in one file like this,

export const SOME_STRING = 'Some value';
export const ANOTHER_STRING = 'Another value';
...

and imports whenever i needed.

So these are my questions...

1) Is this a good approach ?

2) Is there any alternative to this ?

like image 480
theapache64 Avatar asked Jun 01 '18 06:06

theapache64


People also ask

How do you use strings in React Native?

Use the + operator to concatenate two string variables in React Native: const helloWorld = hello + world; Another version with a hardcoded string: const helloWorld2 = hello + 'World!

What is DOM manipulation in React Native?

It is sometimes necessary to make changes directly to a component without using state/props to trigger a re-render of the entire subtree. When using React in the browser for example, you sometimes need to directly modify a DOM node, and the same is true for views in mobile apps.

Does React Native still use bridge?

They are working on a whole new architecture for React Native that eliminates the requirement to use the bridge. They are implementing something called the JavaScript Interface, or JSI, which will sit between the JavaScript code and the JavaScript engine.


1 Answers

You don't need to export each value. One better way I know is to export

const SOME_STRING = 'Some value';
const ANOTHER_STRING = 'Another value';

module.exports = {
 SOME_STRING:SOME_STRING,
 ANOTHER_STRING:ANOTHER_STRING
}

Or you may like to wrap all of this in 1 constant object

const APPLICATION_CONSTANTS = {
    SOME_STRING : 'Some string',
    ANOTHER_STRING : 'Another string'
}

export default APPLICATION_CONSTANTS;

Usage

import APPLICATION_CONSTANTS from './strings';

APPLICATION_CONSTANTS.SOME_STRING
like image 61
Manoz Avatar answered Sep 27 '22 23:09

Manoz