[英]I can't init this array React Native
Everytime i launch the expo render the state array person2 don't init and i have an "can't find variable person2" error when person2 is init the code launch normaly but when i relaunch the app person2 is undefined每次我启动世博会时,状态数组 person2 都不会初始化,并且当 person2 初始化时,我有一个“找不到变量 person2”错误,代码正常启动,但是当我重新启动应用程序时,person2 是未定义的
my person.js :我的 person.js :
export default class Example1 extends React.Component {
constructor (props) {
super (props)
this.state = {
nameres: "",
person2 : []
}
}
setRes (nameres) {
this.setState ({nameres})
person2 = [];
for (let i=0; i<person.length; i++) {
list = person[i].first_name.toUpperCase();
if (list.includes(nameres.toUpperCase())) {
person2.push(person[i])
}
this.setState({ person2 });
}}
render() {
const image = { uri: "../img/23.jpg" };
return (
<View style={styles.main_container}>
<ImageBackground source={image} style={styles.image} blurRadius={5}>
<TextInput style={styles.textinput} placeholder=' recherche par nom' onChangeText={(text) => this.setRes(text)}
value={this.state.nameres}/>
<FlatList
data={this.state.person2}
extraData={person2}
keyExtractor={(item) => item.id.toString()}
renderItem={({item}) => <ItemPerson contid={item}/>}
/>
</ImageBackground>
</View>
);
}
}
my json array :我的 json 数组:
{ "person" : [
{
"id": 1,
"first_name": "jonas",
"last_name": "jojo"
},
{
"id": 2,
"first_name": "Pataa",
"last_name": "tee"
}]}
This might help这可能有帮助
setRes (nameres) {
const person2 = []; // initialize here and add let or const
...
}
You can call setRes
in componentDidMount
which executes when screen renders like您可以在
componentDidMount
中调用setRes
,它会在屏幕呈现时执行,例如
componentDidMount(){
this.setRes(""); // set default
}
in render
在
render
render() {
const image = { uri: "../img/23.jpg" };
return (
<View style={styles.main_container}>
....
<FlatList
...
extraData={this.state.person2} // add `person2` from `state` like
......
/>
</View>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.