[英]Switch screen on React Native
I'm new to rEact Native, and I'm building a switch for a list application for the products that are needed in the supermarket.我是 rEact Native 的新手,我正在为超市所需产品的列表应用程序构建一个开关。
One of the views is done, which is the entry of the products, but I want to make a larger view of this, for that I enable a button in which it has an event called onSelectedEnlage which I call from the first view which is called from the app.js there I transferred it to a function, but I get that it does not find the onSelectedEnlarge function其中一个视图已完成,这是产品的条目,但我想对此进行更大的查看,因为我启用了一个按钮,其中有一个名为 onSelectedEnlage 的事件,我从第一个视图调用该事件称为从 app.js 那里我将它转移到 function,但我知道它没有找到 onSelectedEnlarge function
Can you help me understand this mess你能帮我理解这个烂摊子吗
Thank you谢谢
Share code分享代码
Supermarketlist(first screen)超市列表(第一屏)
import { ActivityIndicator, FlatList, Image, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import React, { useRef, useState } from 'react';
import Buttons from '../components/buttons';
import Color from '../constants/colors';
import Menu from '../components/menu';
import Pickers from '../components/Pickers';
import TT from '../components/textBox';
export default function SupermarketList() {
//useState
const [product, setProduct] = useState('');
const [quantity, setQuantity] = useState(0);
const [measure, setMeasure] = useState('Seleccione una ...');
const [order, setorder] = useState([]);
const [selected, setSelected] = useState(false);
//funciones
const onChangeProduct = (text) => { setProduct(text) };
const onChangeQuantity = (text) => { setQuantity(text) };
const onPressButton = () => {
if (product != '' && quantity != 0 && measure != 'Seleccione una ...') {
setorder([...order, { id: Date.now().toString(), product: product, quantity: quantity, measure: measure, status: false }]);
setProduct('');
setQuantity(0);
setMeasure('Seleccione una ...');
}
};
const renderItem = ({item}) => (
<View style={styles.itemContainer}>
<Text style={ item.status == false ? styles.itemText : styles.itemTextTachado}>{item.product + ' - ' + item.quantity + ' - ' + item.measure}</Text>
<View style={styles.itemButtons}>
<TouchableOpacity onPress={() => addStatus(item.id)}>
<Text style = { styles.itemTextButton}>X</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => deleteItem(item.id)}>
<Text style = { styles.itemTextButton}>Del...</Text>
</TouchableOpacity>
</View>
</View>
)
const addStatus = (id) => {
const newOrder = order.map((item) => {
if (item.id == id) {
item.status = !item.status;
}
return item;
});
setorder(newOrder);
};
const deleteItem = (id) => {
const newOrder = order.filter((item) => item.id != id);
setorder(newOrder);
};
const onPressEnlarge = () => {
onSelectedEnlarge(!selected);
};
//useRef
const pickerRef = useRef();
function open() {
pickerRef.current.focus();
}
function close() {
pickerRef.current.blur();
}
return (
<View style={styles.container}>
<Menu />
<View style={styles.textNomList}>
<Text style={styles.textNom}> Supermercado </Text>
</View>
<View style={styles.textContainerTitle}>
<Text style={styles.textTitle}>Lista de Supermercado </Text>
</View>
<View style={styles.TextInputContainer}>
<View style={styles.TextBoxContainer}>
<TT placeholder="Producto" value={product} onChangeText={onChangeProduct}/>
<TT placeholder="Cantidad" value={quantity} onChangeText={onChangeQuantity}/>
</View>
<Text style={styles.textPicker}> Medidas - {measure}</Text>
<Pickers stateFirst={measure} stateSecond={setMeasure}></Pickers>
</View>
<View style={styles.buttonContainer}>
<Buttons title="Agregar" bkcolor={Color.primary} color={Color.letter} onPress={onPressButton} />
</View>
<View style={styles.buttonContainer2}>
<Buttons title="Ampliar" bkcolor={Color.primary} color={Color.letter} onPress={onPressEnlarge} />
</View>
<FlatList
data={order}
renderItem={renderItem}
keyExtractor = {(item )=> item.id.toString()}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: Color.primary,
color: Color.letter,
fontFamily: 'Lato-Regular',
},
textNomList: {
marginLeft: 20,
alignItems: 'baseline',
fontFamily: 'Lato-Regular',
},
textNom: {
color: Color.letter,
fontSize: 10,
fontWeight: 'bold',
},
textContainerTitle: {
marginTop: 10,
marginLeft: 20,
alignItems: 'center',
},
textTitle: {
color: Color.letter,
fontSize: 30,
fontWeight: 'bold',
},
textPicker: {
color: Color.letter,
},
TextInputContainer: {
marginTop: 40,
marginHorizontal: 20,
},
TextBoxContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
},
TextInput: {
height: 33,
color: Color.primary,
backgroundColor: Color.letter,
fontSize: 15,
selectionColor: Color.letterº,
placeholderTextColor: Color.primary,
marginBottom: 15,
paddingHorizontal: 10,
},
buttonContainer: {
width: '100%',
alignItems: 'center',
marginTop: 20,
marginBottom: 10,
height: 33,
},
buttonContainer2: {
width: '100%',
alignItems: 'center',
marginBottom:15,
height: 33,
},
itemButtons: {
flexDirection: 'row',
justifyContent: 'space-between',
width: 60,
},
itemContainer: {
backgroundColor: Color.letter,
padding: 15,
marginVertical: 8,
marginHorizontal: 20,
color: Color.primary,
fontSize: 15,
borderRadius: 5,
flexDirection: 'row',
justifyContent: 'space-between',
},
itemText: {
textDecorationLine:'none',
},
itemTextTachado: {
textDecorationLine:'line-through',
},
itemTextButton: {
color: Color.primary,
fontSize: 15,
fontWeight: 'bold',
},
});
app.js应用程序.js
import {ActivityIndicator, StyleSheet, View} from 'react-native';
import React, { useState } from 'react';
import Color from './constants/colors';
import { StatusBar } from 'expo-status-bar';
import Super from './screens/supermarketList';
import SuperEnlage from './screens/supermarketListEnlage';
import { useFonts } from 'expo-font';
export default function App() {
//useState
const [selected, setSelected] = useState(false);
//funciones
const [loaded] = useFonts({
'Lato-Regular': require('./assets/fonts/Lato-Regular.ttf'),
'Lato-Bold': require('./assets/fonts/Lato-Bold.ttf'),
'Lato-Light': require('./assets/fonts/Lato-Light.ttf'),
'Lato-Italic': require('./assets/fonts/Lato-Italic.ttf'),
'Lato-Black': require('./assets/fonts/Lato-Black.ttf'),
});
if(!loaded) {
return (
<View style={styles.containerLoader}>
<ActivityIndicator size="large" color={Color.letter} />
</View>
)
}
let content = <SuperEnlage />;
const onSelectedEnlarge = (select) => {
setSelected(select);
};
if (selected) {
content = <SuperEnlage />;
}
else
{
content = <Super />;
}
return (
<View style={styles.container}>
{content}
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: Color.primary,
color: Color.letter,
fontFamily: 'Lato-Regular',
},
});
I think that you would like to call a function in parent component.我认为您想在父组件中调用 function 。 I suggest you to pass
onSelectedEnlarge()
in App.js
as a props to the child component Supermarketlist.js
.我建议您将
App.js
中的onSelectedEnlarge()
作为道具传递给子组件Supermarketlist.js
。
In App.js在 App.js 中
const onSelectedEnlarge = (select) => {
setSelected(select);
};
if (selected) {
content = <SuperEnlage />;
}
else
{
//Pass your function as props
content = <Super onSelectedEnlarge={(select)=>{onSelectedEnlarge(select)}} />;
}
In Supermarketlist(first screen)在超市列表中(第一个屏幕)
//Get the props pass from parent component
export default function SupermarketList({onSelectedEnlarge}) {
const [selected, setSelected] = useState(false);
...
const onPressEnlarge = () => {
//Call the parent function
onSelectedEnlarge(!selected);
};
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.