我想用服务器中的文本更改应用程序中的所有静态文本,因此我必须在x2^2 > c^2 - y1^2
中获取此文本,然后将其导出以在其他屏幕中使用
LoadingScreen
到<Text>My products</Text>
如何将json响应从<Text>{constants.myProduct}</Text>
导出到所有其他屏幕?
还有另一种方法吗?
PS。我不使用LoadingScreen
答案 0 :(得分:1)
最简单的方法,但不是最好的方法,就是使用所有文本键创建和导出变量。例如:
// text.utils.js
let _textData;
export const loadTextData = () => fetch(YOUR_SERVER)
.then((r) => r.json())
.then(r => _textData = r);
export const getTextData = (key, defaultValue) => _textData[key] || defaultValue;
// init inside App or something else
import {loadTextData} from 'text.utils.js';
componentDidMount() {
loadTextData();
}
// inside components
import {getTextData} from 'text.utils.js';
const myProductText = getTextData('myProduct', 'This is product text');
const myProductCountText = getTextData('myProductCount', 'This is product count text');
<Text>{myProductText}</Text>
<Text>{myProductCountText}</Text>
P.S。这不是反应的好方法。最好的选择是创建一个上下文或挂钩,它将为您的组件提供字符串。另外,如果您的字符串对象包含嵌套对象,则可以重新组织getTextData
函数