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 ?
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!
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With