I am new to React/React-native. I am trying to add a key-value pair in the JSON body which is the output of screen 1 having the following components:
The components on screen 2 are:
Initially, the JSON data produced from screen 1 WITHOUT the functionality of the Types button was:
body: JSON.stringify({
name: Name,
email: Email})
but now I want the output from screen 1 to be:
body: JSON.stringify({
name: Name,
email: Email,
types:[type1:"a", type2:"b"]
})
where type1 and type2 are the labels of the two dropdown lists and the options selected are "a" and "c" respectively after pressing a SAVE button on screen 2 and then navigating to screen 1. I have used the following useState methods for name and email for screen1:
const [Name, setName] = useState("")
const [Email, setEmail] = useState("")
and after making Types as a functional button on screen 1:
const [Name, setName] = useState("")
const [Email, setEmail] = useState("")
const [Types, setTypes] = useState([])
The below code is to update the name and the email with a Types button on screen 1:
<TextInput
label='Name'
value={Name}
onChangeText={text => setName(text)}
/>
<TextInput
label='Email'
value={Email}
onChangeText={text => setEmail(text)}
/>
<Button onPress={() => navigation.navigate("screen_2")>
Types
</Button>
What should be my onPress function in the button, Types(Screen 1) and button, Save(Screen 2) and how do I update the JSON body after saving my options on screen 2?
If you're using something like React Router, you should check how to pass state with it. If not you might want to create a Redux store to save state relevant to the whole application