【发布时间】:2022-01-13 04:52:59
【问题描述】:
我的第一页将包含一些数据的数组推送到下一页,我想在 RenderItem 中使用该数据作为平面列表。我尝试了绑定事件,但它对我也不起作用,任何帮助都会得到帮助。
enter code here
导出默认类 mainPage 扩展 React.Component
constructor(props: any) {
super(props)
this.state = {
text: '',
slots: [],
screen: 'screen1',
vehicleNumber: '',
parkingLot: [],
selectedSlot: '',
}
}
createSlot() {
let tempdata: any[] = []
for (let i = 0; i < parseInt(this.state.text); i++) {
tempdata.push({
allocated: false,
timestamp: new Date(),
})
}
this.setState(
{
slots: [
...this.state.slots,
{
id: (Math.random() + 1).toString(36).substring(7),
count: parseInt(this.state.text),
data: tempdata,
},
],
text: '',
screen: 'screen2',
},
() => {
this.props.navigation.navigate('floor', {
slots: this.state.slots,
})
},
)
}
// componentDidMount() {
// this.setState({ parkingLot: [...this.parkingLot] })
// }
park = () => {
console.log('jcjhj', this.state.parkingLot)
var item = this.state.slots[
Math.floor(Math.random() * this.state.slots.length)
]
console.log('Parkinglot', this.state.parkingLot, item)
console.log('Slots', this.state.slots)
if (this.state.slots) {
}
// this.setState({
// parkingLot: [
// ...this.state.parkingLot,
// { carnumber: this.state.vehicleNumber },
// ],
// })
}
renderItem(item: any) {
return (
<TouchableOpacity
style={styles.Slotinput}
onPress={() =>
this.setState({ screen: 'screen3', selectedSlot: item.id })
}
>
<Text style={{ fontSize: 12, color: 'white', fontWeight: 'bold' }}>
Slot ID:-{item.id}
</Text>
<Text style={{ fontSize: 12, color: 'white', fontWeight: 'bold' }}>
Slot Count:-{item.count}
</Text>
</TouchableOpacity>
)
}
renderItem1(item: any) {
return (
<View style={styles.Slotinput}>
<Text>{item.allocated ? 'Allocated' : 'Available'}</Text>
{this.state.parkingLot.map((e: any) => {
return <Text>{e.carnumber}</Text>
})}
</View>
)
// )
// }
// return demo
}
render() {
return (
<View>
{this.state.screen === 'screen1' && (
<View style={styles.form}>
<TextInput
style={styles.input}
placeholder="Enter A value for slot"
onChangeText={(text: any) => this.setState({ text })}
value={this.state.text}
/>
<TouchableOpacity
style={
this.state.text && this.state.text
? styles.addItemButton
: styles.BlurItemButton
}
disabled={this.state.text == ''}
onPress={() => this.createSlot()}
>
<Text style={styles.buttonText}>Submit</Text>
</TouchableOpacity>
</View>
)}
{/* {this.state.screen === 'screen2' && (
<>
<Text onPress={() => this.setState({ screen: 'screen1' })}>
Back
</Text>
<FlatList
data={this.state.slots}
renderItem={({ item }) => this.renderItem(item)}
keyExtractor={(item) => item.id}
/>
</>
)} */}
{/* {this.state.screen === 'screen3' && (
<>
<Text onPress={() => this.setState({ screen: 'screen2' })}>
Back
</Text>
<>
{}
<TextInput
placeholder="Vechicle Number"
style={styles.input}
value={this.state.vehicleNumber}
onChangeText={(vehicleNumber: any) =>
this.setState({ vehicleNumber })
}
// onChangeText={data => this.setState({ vehicleNumber: data })}
></TextInput>
</>
<TouchableOpacity
style={styles.addItemButton}
onPress={() => this.park()}
>
<Text>Park Vechicle</Text>
</TouchableOpacity>
{this.state.slots
?.filter((i: any) => i.id === this.state.selectedSlot)
?.map((item: any, index: number) => {
return (
<View key={index}>
<FlatList
data={item.data}
renderItem={({ item }) => this.renderItem1(item)}
keyExtractor={(item, index) => index.toString()}
/>
</View>
)
})}
</>
)} */}
</View>
)
}
}
Here is my floor.tsx page(next page)
在此处输入代码
export default class floor extends Component
this.state = {
text: '',
slots: [],
screen: 'screen1',
selectedSlot: '',
}
// let slo: Array<any> = this.props.route.params
// console.log('propdata-->', slo)
// this.setState({
// slo: [],
// })
}
renderItem=(item: any)=> {
console.log('item', item)
return (
<TouchableOpacity
style={styles.Slotinput}
onPress={() =>
this.setState({ screen: 'screen3', selectedSlot: item.id })
}
>
<Text style={{ fontSize: 12, color: 'white', fontWeight: 'bold' }}>
Slot ID:-{item.id}
</Text>
<Text style={{ fontSize: 12, color: 'white', fontWeight: 'bold' }}>
Slot Count:-{item.count}
</Text>
</TouchableOpacity>
)
}
render() {
return (
<View>
<Text onPress={() => this.props.navigation.goBack()}>Back</Text>
<FlatList
data={this.props.route.params}
renderItem={({ item }) =>
this.renderItem(item)}
keyExtractor={(item) => item.id}
/>
</View>
)
}
}
【问题讨论】:
-
什么是 props.route.params 打印?必须有另一个变量名对吗? this.props.route.params.slots ?
-
是的..它的 props.route.params.slots
-
问题解决了吗?
-
yesssss....我把 while 放在 Data={} 的平面列表中
-
先生终于找到了解决方案......现在我想在该数组中插入 textinput 的值
标签: javascript typescript react-native class react-native-flatlist