Javascript 在安装组件之前,如何在React Native中区分语言?
我希望这样的事情能奏效。这不是因为this.text.瞳孔未定义Javascript 在安装组件之前,如何在React Native中区分语言?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我希望这样的事情能奏效。这不是因为this.text.瞳孔未定义 是因为组件Willmount吗 如果是,如何在render()函数之外区别语言?因为在这种情况下,我有不同角色和语言的人来呈现,而且因为我是一个新手,反应是本地的,所以这是我见过的唯一“漂亮”的解决方案 。。。 从“../../../../../methods/checkLanguage”导入{t}; ... 班级学校扩展组件{ 建造师(道具){ 超级(道具); } 组件willmount(){ this.text=t(“建筑物”
。。。
从“../../../../../methods/checkLanguage”导入{t};
...
班级学校扩展组件{
建造师(道具){
超级(道具);
}
组件willmount(){
this.text=t(“建筑物”);
}
人=[
{
姓名:“马克斯·穆斯特曼”,
角色:this.text.小学生,
图像:需要(“../../../../max.jpg”)
}, ....
]
render(){
返回(
.....
{this.people.map((项目,索引)=>(
))}
.....)
}
这就是您如何定义和使用它的方法。尽管遵循一些教程并按照注释中的建议充分理解代码总是更好的
constructor(props) {
super(props);
var text = t("Buildings"); // I don't know what is t() here.
this.state = {
text : text,
people : [{
name: "Max Mustermann",
role: text.pupil,
image: require("../../../../..max.jpg")
}]
}
}
render() {
const {people} = this.state
return (
.....
{ people.map((item, index) => (
<ImageName key={index} name={item.name} role={item.role} image={item.image} />
))
}
.....)
}
构造函数(道具){
超级(道具);
var text=t(“建筑物”);//我不知道这里的t()是什么。
此.state={
文本:文本,
人民:[{
姓名:“马克斯·穆斯特曼”,
角色:text.小学生,
图像:需要(“../../../../max.jpg”)
}]
}
}
render(){
const{people}=this.state
返回(
.....
{people.map((项目,索引)=>(
))
}
.....)
}
为什么要将人员
定义为类属性?为什么要使用组件willmount
(该组件已弃用至少一年)。为什么不直接在构造函数中调用t
?到底什么是t
,为什么要给函数一个单字符的名称?为什么要在定义类属性时定义一个实例变量,它甚至在装入之前都没有设置?为什么……这里有太多的东西来撬开你真正想要或想要的ying to do。上面的注释包含正确答案。this.text.poudio是一个实例变量,在调用构造函数之前,它不会在类定义中设置。也许您可以添加people=[…]将学生作为参数传递给构造函数。这是我大学以前的学生编写的代码,我应该开发这个应用程序。因为我是新手,我不知道现有的代码有多好。谢谢你的想法!@Shamray这很糟糕,真的,真的,真的很糟糕。你最好通过t然后再继续做这件事。
constructor(props) {
super(props);
var text = t("Buildings"); // I don't know what is t() here.
this.state = {
text : text,
people : [{
name: "Max Mustermann",
role: text.pupil,
image: require("../../../../..max.jpg")
}]
}
}
render() {
const {people} = this.state
return (
.....
{ people.map((item, index) => (
<ImageName key={index} name={item.name} role={item.role} image={item.image} />
))
}
.....)
}