如何在本地响应中全局声明字符串

N S*_*rma 6 string android react-native

我的react-native应用程序中有很多字符串.我看到一些字符串正在多个地方使用.IMO我不想在我的代码中硬编码字符串,因为这不是好方法.如果项目大规模进行,可能需要很长时间才能在多个位置更改相同的字符串.

什么方法是声明反应本机应用程序的字符串.它有android开发的地方strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string
        name="string_name"
        >text_string</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

我在JS文件中做了什么.

<Text style={{ fontSize: 22, textAlign: "center" }}>
  Never forget to stay in touch with the people that matter to you.
</Text>
Run Code Online (Sandbox Code Playgroud)

Mot*_*Azu 7

React Native像你一样,没有专门的字符串资源管理器Android.您可以将一个包含所有这些常量的文件导出,并将其导入到您需要的任何位置.这些方面的东西:

**** constants.js

export const NeverForget = 'Never forget to stay in touch with the people that matter to you.';
export const OtherString = 'Welcome to the app';

**** Usage:

import { NeverForget } from 'app/constants';

...
<Text style={{ fontSize: 22, textAlign: "center" }}>
  { NeverForget }
</Text>

**** Or

import * as constants from 'app/constants';

...
<Text style={{ fontSize: 22, textAlign: "center" }}>
  { constants.NeverForget }
</Text>
Run Code Online (Sandbox Code Playgroud)