2017-04-25 53 views
1

我的react-native應用程序中有很多字符串。我看到一些字符串正在多個地方使用。國際海事組織我不想在我的代碼中硬編碼字符串,因爲這不是好方法。如果項目大規模進行,可能需要很長時間才能在多個地點更改相同的字符串。如何在原生反應中聲明全局字符串

什麼方法是聲明反應原生應用程序的字符串。我做Android開發的地方有strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string 
     name="string_name" 
     >text_string</string> 
</resources> 

我在JS文件做什麼。

<Text style={{ fontSize: 22, textAlign: "center" }}> 
    Never forget to stay in touch with the people that matter to you. 
</Text> 
+0

你可以只是有一個常量對象,你導入的任何地方,你需要這些字符串 –

+0

@MotiAzu你能解釋一段代碼pls嗎? –

回答

1

沒有爲字符串沒有專門的資源管理器中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>