Multiple Input Text handle in object and array in react native

1.1k views Asked by At

This is my code .... react native Multiple Input Text handle in object and array in react native... Is it possible to share your code? Share the entire component where you have these groups of 5 inputs. You can just copy and paste it inside your question body. That way I can help you better

App.js

        import {
          StyleSheet,
          Pressable,
          View,
        
        } from 'react-native';
        import {
          AntDesign,
          FontAwesome
        } from '@expo/vector-icons';
        import { useState } from 'react';
        
        import AddStudentProfile from './Pages/AddStudentProfile';
        import StudentList from './Pages/StudentList';
        
        export default function App() {
          const [iconColor, setIconColor] = useState(['#ab09bf', '#A9A9A9']);
          const [multiValues, setMultiValues] = useState([]);
          const [screen, setScreen] = useState(<StudentList stuList={multiValues} />);
        
        
          function changeIconColor(pressCheck) {
            if (pressCheck) {
              setIconColor([
                '#ab09bf',
                '#A9A9A9'
              ]
              );
            }
            else {
              setIconColor([
                '#A9A9A9',
                '#ab09bf'
              ]
              );
            }
        
          }
          // const [iconChangableColor, setIconChangableColor] = useState('#A9A9A9');
          function changeScreen(scr) {
            setScreen(
              // < AddStudentProfile />
              scr
            );
            // setIconChangableColor('#ab09bf');
            // console.log(multiValues)
            changeIconColor(true)
          }
        
          function appendData(inp, onsrcChange) {
        
            setScreen(
              // < AddStudentProfile />
              onsrcChange
            );
            setMultiValues([
              ...multiValues,
              inp]);
            console.log(multiValues)
          }
          return (
            < View style={styles.container} >
        
              <View style={{ flex: 9 }}>{screen}
        
              </View>
              <View >
                <View style={styles.bottomIconContainer}>
                  <Pressable onPress={() => [changeScreen(<StudentList />), changeIconColor(true)]}
                    android_ripple={{ color: 'black' }}>
                    <View style={styles.bottomIconInnerContainer}>
                      <FontAwesome
                        name="list-ul"
                        size={35}
                        color={iconColor[0]} />
                    </View></Pressable>
        
                  <Pressable onPress={() => [changeScreen(<AddStudentProfile onAppendData={appendData} returnToProfile={changeScreen} />), changeIconColor(false)]}
                    android_ripple={{ color: 'black' }}>
                    <View style={styles.bottomIconInnerContainer}>
                      <AntDesign name="adduser"
                        size={35}
                        color={iconColor[1]}
                      /></View></Pressable></View>
              </View>
            </View >
          );
        }
        
        const styles = StyleSheet.create({
          container: {
            flex: 1,
            backgroundColor: '#fff',
          },
          bottomIconContainer: {
            flexDirection: 'row',
            justifyContent: 'space-around',
            alignItems: 'center',
        
          },
          bottomIconInnerContainer: {
            marginVertical: 20,
            marginHorizontal: 80
          }
        });
        
        

AddStudentProfile.js

        import {
            Text,
            TextInput,
            ImageBackground,
            View,
            Button,
            ScrollView,
            StatusBar,
            StyleSheet
        } from "react-native";
        import { useState } from "react";
        import { AntDesign } from '@expo/vector-icons';
        import PrimaryButton from "../Componenets/PrimaryButton";
        import ColorCode from "../Componenets/ColorCode.js";
        
        export default function AddStudentProfile({ returnToProfile, onAppendData }) {
        
            const [values, setValues] = useState({});
            function inputHandler(name, value) {
                setValues({
                    ...values,
                    [name]: value
                })
            }
            function inpValues(srcChange) {
                onAppendData(values, srcChange)
                console.log(values)
            }
            return (
                <ScrollView>
                    <View style={styles.screenContainer}>
                        <View>
                            <Text style={styles.textContainer}>
                                Add Student Profile
        
                            </Text>
                        </View>
                        <View style={styles.iconOutterContainer}>
                            <View style={styles.iconContainer}>
                                <AntDesign
                                    name="user"
                                    size={80}
                                    color='white'
                                />
                            </View>
                        </View>
                        <View style={{ alignItems: 'center' }}>
                            <TextInput
                                style={styles.inputTextContainer}
                                placeholder="name"
                                placeholderTextColor={ColorCode.placeHolder}
                                onChangeText={(val) => inputHandler('sname', val)}
                            />
                            <TextInput
                                style={styles.inputTextContainer}
                                placeholder="roll no"
                                placeholderTextColor={ColorCode.placeHolder}
                                onChangeText={(val) => inputHandler('rno', val)}
                            />
                            <TextInput
                                style={styles.inputTextContainer}
                                placeholder="department"
                                placeholderTextColor={ColorCode.placeHolder}
                                onChangeText={(val) => inputHandler('dep', val)}
                            />
                            <TextInput
                                style={styles.inputTextContainer}
                                placeholder="e-mail"
                                placeholderTextColor={ColorCode.placeHolder}
                                onChangeText={(val) => inputHandler('mail', val)}
                            />
                            <TextInput
                                style={styles.inputTextContainer}
                                placeholder="Phone no"
                                placeholderTextColor={ColorCode.placeHolder}
                                onChangeText={(val) => inputHandler('phno', val)}
                            />
                        </View>
                        <PrimaryButton
                            onreturnToProfile={returnToProfile}
                            inputValues={inpValues}
                            changeColor='#8a0896'
                        >Save</PrimaryButton>
                    </View>
                </ScrollView>
            );
        }
        
        const styles = StyleSheet.create({
            iconContainer: {
                height: 100,
                width: 100,
                borderRadius: 100,
                backgroundColor: '#ab09bf',
                alignItems: 'center',
                justifyContent: 'center'
            },
            textContainer: {
                marginVertical: 10,
                textAlign: 'center',
                fontSize: 20,
            },
            screenContainer: {
                marginTop: StatusBar.currentHeight,
                flex: 1,
                padding: 20
            },
            iconOutterContainer: {
                alignItems: 'center'
            },
            inputOutterContainer: {
                padding: 10,
                marginHorizontal: 5
            },
            inputTextContainer: {
                padding: 10,
                backgroundColor: '#fff',
                marginVertical: 10,
                width: '95%',
                fontSize: 19,
                elevation: 5,
                borderRadius: 6,
                shadowColor: '#ab09bf',
                color: '#ab09bf'
            },
            buttonOutterContainer: {
                width: '30%',
                marginHorizontal: 10,
                fontSize: 20
            },
            buttonInnerContainer: { fontSize: 23 }
        });
        
        
         PrimaryButton.js
        
            import {
            View,
            Text,
            Pressable,
            StyleSheet
        } from 'react-native';
        import StudentList from '../Pages/StudentList';
        
        
        export default function PrimaryButton({ children, inputValues, onreturnToProfile }) {
            function pressHandler() {
        
                //onreturnToProfile();
                inputValues(<StudentList />)
        
        
            }
            return (
        
                < View style={{ alignItems: 'center', marginTop: 15 }
                }>
                    <View
                        style={styles.textOutterContainer}
                    >
                        <Pressable
                            onPress={pressHandler}
                            android_ripple={{ color: 'white' }}
                        >
                            <Text style={styles.textContainer}>{children}</Text>
                        </Pressable>
                    </View>
                </View >
            );
        }
        
        const styles = StyleSheet.create({
            textContainer: {
                fontSize: 23,
                color: 'white',
                textAlign: 'center'
            },
            textOutterContainer: {
                backgroundColor: '#8a0896',
                borderRadius: 22,
                width: '20%',
                height: 40,
                alignItems: 'center',
                justifyContent: 'center'
            }
        })
1

There are 1 answers

0
2xSamurai On

I have gone through your code and it seems to be right even though a bit complicated. What is the issue you are facing?

You are adding a set of student info(5 fields) on a button press to a parent component state. You are appending it to an empty array. Ideally, you should get something like the below. What seems to be the problem? Please explain.

multiValues = [
    {
        'sname': 'some sname',
        'rno': 'some rno',
        'dep': 'some dep',
        'mail': 'some mail',
        'phno': 'some phno'
    },
    {
        'sname': 'some sname',
        'rno': 'some rno',
        'dep': 'some dep',
        'mail': 'some mail',
        'phno': 'some phno'
    },
    {
        'sname': 'some sname',
        'rno': 'some rno',
        'dep': 'some dep',
        'mail': 'some mail',
        'phno': 'some phno'
    },
    {
        'sname': 'some sname',
        'rno': 'some rno',
        'dep': 'some dep',
        'mail': 'some mail',
        'phno': 'some phno'
    },
]

Will update this answer depending on your response so that it might be of help to someone else.