在这里试图做的是,在选择器值更改上调用该函数并试图呼叫循环并绘制5个文本输入,但会出现错误。当我试图直接在价值上调用myloop数组时,请帮助解决这个小错误,以更改其生成错误,该错误无法称呼为函数。
import * as React from 'react';
import { Text, View, StyleSheet, Alert, Picker } from 'react-native';
import { Constants } from 'react-native';
import { TextInput } from 'react-native-gesture-handler';
var myloop = [];
;
export default class UiDynamic extends React.Component {
// add a selectValue to your state to stop the overwriting
state = {
PickerValueHolder: [],
selectedValue: ''
}
componentDidMount() {
// remove the return
fetch('http:///Dsenze/userapi/inventory/viewinventorytype', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"username" :"admin",
"password" :"admin"
})
}).then((response) => response.json())
.then((responseJson) => {
// use the inventoryTypeData as it is already an array
let PickerValueHolder = responseJson.inventoryTypeData;
console.log("Array data" , PickerValueHolder)
this.setState({ PickerValueHolder }); // Set the new state
})
.catch((error) => {
console.error(error);
});
}
onPickerValueChange=()=>{
{myloop}
console.log("Picker change" , myloop);
// this.onPickerValueTextinput();
}
render() {
for (let i = 0; i < 5; i++) {
myloop.push(
<View key={<TextInput></TextInput>}>
<TextInput style={{ textAlign: 'center', marginTop: 5 }}
placeholder="Enter your name ">
</TextInput>
</View>
)
}
return (
<View style={styles.container}>
{/* {myloop} */}
{<Picker
selectedValue={this.state.selectedValue}
onValueChange={this.onPickerValueChange}>
{ this.state.PickerValueHolder.map((item, key)=>
<Picker.Item label={item.name} value={item.name} key={key} />
)}
</Picker>}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
}
});
谢谢
您的目标渲染5个文本输入
和您所在州的布尔值
state = {
PickerValueHolder: [],
selectedValue: '',
isChange : false,
}
您的Onchange功能,应用任何条件来通过设置布尔值来渲染这些项目:
onPickerValueChange=(itemValue, itemPosition)=>{
this.setState({isChange:true});
}
制作一个函数显示项目,呈现5个输入;
const displayItems = () => {
if (this.state.isChange) {
return for (let i=0; i<5; i++) {
return (
<View key={<TextInput></TextInput>}>
<TextInput style={{ textAlign: 'center', marginTop: 5 }}
placeholder="Enter your name ">
</TextInput>
</View>
);
}
}
};
渲染
return (
<View style={styles.container}>
{displayItems()}
<Picker
selectedValue={this.state.selectedValue}
onValueChange={this.onPickerValueChange}>
{ this.state.PickerValueHolder.map((item, key)=>
<Picker.Item label={item.name} value={item.name} key={key} />
)}
</Picker>
</View>
);