我在屏幕之间传递数据时遇到了问题。 这是我输入的屏幕:
import React, {useState} from 'react'
import {View, TextInput, Button, StyleSheet, TouchableWithoutFeedback, Keyboard} from 'react-native'
import Card from '../components/Card'
import colors from '../constants/colors';
type Props={
navigation:any
}
const AddingScreen = (props:Props) =>{
let annmountAndProductToSend:any;
const [enteredProduct, setEnteredProduct] = useState<string>('');
const [amount, setAmount] = useState<any>('');
function enteredProductHandler(enteredText:string):void
{
setEnteredProduct(enteredText);
}
function amountHandler(enteredText:any):void
{
let amountAndProduct;
const amm = parseInt(enteredText);
if(isNaN(enteredText) || enteredText > 1)
{
setAmount(enteredText);
amountAndProduct = enteredText + ' ' + enteredProduct;
annmountAndProductToSend = amountAndProduct;
}
else setAmount('')
}
function confirmProduct()
{
props.navigation.navigate('Main',{input:enteredProduct});
setEnteredProduct('');
setAmount('');
}
function returnToMainScreen()
{
props.navigation.navigate('Main')
}
return(
<TouchableWithoutFeedback onPress = {() => {Keyboard.dismiss();}}>
<View style = {styles.inputContainer}>
<Card style = {styles.screen} >
<TextInput blurOnSubmit autoCapitalize="none"
placeholder="Nazwa przedmiotu"
style={styles.input}
onChangeText={enteredProductHandler} //1.onChangeText pobiera wpisany text i wysyła do goalInputHandler
value={enteredProduct}
/>
<TextInput keyboardType = "number-pad"
placeholder="Ilość"
style={styles.input}
onChangeText={amountHandler} //1.onChangeText pobiera wpisany text i wysyła do goalInputHandler
value={amount}
/>
<View style = {styles.buttonContainer}>
<View style = {styles.button}>
<Button color = {colors.accent} title = 'Dodaj' onPress = {confirmProduct}/>
</View>
<View style = {styles.button}>
<Button title = 'Wyjdź' color = {colors.accent} onPress={returnToMainScreen}/>
</View>
</View>
</Card>
</View>
</TouchableWithoutFeedback>
);
};
const styles = StyleSheet.create({
screen:
{
justifyContent:'center',
alignItems:'center',
},
inputContainer:{
flex: 0.5,
justifyContent:'center',
alignItems:'center'
},
input:{
height:30,
borderBottomColor: 'grey',
borderBottomWidth: 1,
marginVertical: 13,
},
buttonContainer:{
flexDirection:'row',
alignItems: 'center',
justifyContent: 'space-between' ,
width: '60%'
},
button:{
width:'40%',
}
});
export default AddingScreen;
我发送的数据正在运行:
function confirmProduct()
{
props.navigation.navigate('Main',{input:enteredProduct});
setEnteredProduct('');
setAmount('');
}
还有第二个屏幕:
import { StatusBar } from 'expo-status-bar';
import React, {useState} from 'react';
import {
StyleSheet,
View,
Button,
VirtualizedList,
Alert,
} from 'react-native';
import GoalItem from'../components/Goalitem';
import Header from '../components/Header';
import colors from '../constants/colors';
type Props={
navigation:any
}
const MainScreen = (props:Props) =>{
//var inputFromAdding = JSON.stringify(props.navigation.getParam('input'));
const [products, setProducts] = useState<any>([]);
const [amount, setAmount] = useState<any>([])
function addProduct(goalTitle: any):any
{
//if(goalTitle.length === 0) return;
setProducts([...products,{id: Math.random().toString(), value: goalTitle}]);
return products;
};
function removeGoalHandler(goalId:any):void
{
setProducts((courseGoals: any[]) =>
{
return courseGoals.filter((goal:any) => goal.id !== goalId);
});
};
function deleteListAlert():void
{
if(products.length >0)
{
Alert.alert(
'Lista zostanie wyczyszczona!',
'Czy aby na pewno chcesz wyczyścić liste?',
[{text: 'Tak', style: 'destructive', onPress: deleteList},
{text: 'Nie', style: 'cancel'}]);
}
else{
Alert.alert(
'Lista jest pusta.',
'W liście nie ma żadnych przedmiotów.',
[{text: 'Powrót', style: 'cancel'}]);
}
}
function deleteList()
{
setProducts('')
}
//let xx = ;
return (
<View style = {styles.screen}>
<Header title="Lista zakupów"/>
<VirtualizedList
keyExtractor={(item:any, index) => item.id}
data ={addProduct(JSON.stringify(props.navigation.getParam('input')))}
renderItem ={itemData => (
<GoalItem
id = {itemData.item.id}
onDelete = {removeGoalHandler}
title = {itemData.item.value}
/>
)}
/>
<View style = {styles.buttonPosition}>
<View style = {styles.button1}>
<Button color = {colors.accent} title = "WYCZYŚĆ" onPress={deleteListAlert}/>
</View>
<View style = {styles.button}>
<Button color = {colors.accent} title="+" onPress = {() => {
props.navigation.navigate('Adding')
}}/>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
screen:{
flex:1,
backgroundColor: 'white',
},
button:{
width: 40,
},
button1:{
width: 90,
},
buttonPosition:{
padding:15,
alignItems: 'stretch',
justifyContent: 'space-between',
flexDirection:'row',
backgroundColor: colors.primary,
},
});
export default MainScreen;
我正在接收数据:
data ={addProduct(JSON.stringify(props.navigation.getParam('input')))}
并将其添加到函数中:
function addProduct(goalTitle: any):any
{
//if(goalTitle.length === 0) return;
setProducts([...products,{id: Math.random().toString(), value: goalTitle}]);
return products;
};
然后返回产品,这是我在下面呈现的值数组。
错误是:组件异常:太可能重新渲染。 React 限制渲染次数以防止无限循环。