Aditya
Aditya

Reputation: 21

TextInput show values from get Api and after update show new values in react native

I have an API that I'm retrieving data from, and I'm setting the data inside my input values, user should be able to edit those inputs and update, but when i try to type in the input it won't type anything and the value stays as it is. for updating data there is post request.

                         <TextInput 
                            style={styles.mailInput}
                            mode={'outlined'}
                            label={'First *'}
                            selectionColor={'#000'}
                            theme={{ colors: {primary: '#D3D3D3'}}}
                            onChangeText={text => setProfile({...editprofile, firstName: text })}
                            placeholder="First Name"
                            value={editprofile.firstName}
                         />


                         const [editprofile, setProfile] = React.useState({
                          company: "",
                          gstNo: "",
                          firstName: "",
                          lastName: "",
                          phone: "",
                          email: "",
                          cinNo: "",
                          state: "",
                          city: "",
                          pincode: "",
                          address: "",
                       });

When user open profile screen it will values getting from api using get request. if user want to update any field, he/she can entered new value in that textinput and click to update button. update call a handleUpdate function that is using post request to api for updating data. after updated new values show on same textinput. So how it will be done ??? I'm using hooks and functional Component in react native.

for getting data 'getdata' is state variable. I'm using like const [getdata, setGetData] = React.useState([]);

Upvotes: 1

Views: 972

Answers (1)

Dhruv Patel
Dhruv Patel

Reputation: 1529

See my example,

const [description, setDescription] = useState("")

<TextInput
    value={description}
    onChangeText={(text) => setDescription(text)} />

Your flow should be,

Call API > Get data > Store data (i.e. description in state variable) > Update same variable

Upvotes: 0

Related Questions