I have a component here called Starters:
const Starters = ({item}) => {
const handleAdd = (data) => {
setHomeData(data);
showHomeModal()
};
const data = {
Name: item.title,
Stack: item.stack,
ScheduleName: item.subtitle,
Track: true,
TrackString: "true",
}
return (
<Card
style={{
marginRight: 10,
marginBottom: 6
}}
>
<Card.Title
title={item.title}
subtitle={`Schedule: ${item.subtitle}`}
right={() =>
<IconButton
icon="plus-circle"
color="#5F9DA5"
onPress={() => handleAdd(data)}
/>
}
rightStyle={{
marginBottom: 22
}}
/>
<Card.Content>
{Object.entries(item.stack.slice(0,3)).map(item => (
<View key={uuidv4()} style={{flexDirection: "row"}}>
<Text
key={uuidv4()}
style={{
fontSize: 12,
marginVertical: -3
}}
>
{item[1].substance}: {item[1].amount}{item[1].measure}{"n"}
</Text>
</View>
))}
</Card.Content>
</Card>
)
}
The item of the component is seen as follows:
const itemData = {
"Vits": {
title: "Morning Essentials",
subtitle: "Everyday",
stack: [
{
substance: "Omega3",
amount: "3900",
measure: "mg",
id: uuidv4()
},
{
substance: "B12",
amount: "25",
measure: "mg",
id: uuidv4()
},
{
substance: "B-complex",
amount: "2",
measure: "Tablets",
id: uuidv4()
}
]
},
"Vits2": {
title: "Evening Essentials",
subtitle: "Everyday",
stack: [
{
substance: "Vitamin C",
amount: "500",
measure: "mg",
id: uuidv4()
},
{
substance: "Calcium Citrate",
amount: "500",
measure: "mg",
id: uuidv4()
},
{
substance: "D3",
amount: "16000",
measure: "IU",
id: uuidv4()
},
{
substance: "Magnesium",
amount: "80",
measure: "mg",
id: uuidv4()
},
{
substance: "Zinc",
amount: "10",
measure: "mg",
id: uuidv4()
},
{
substance: "Multi-vitamin",
amount: "1",
measure: "Tablets",
id: uuidv4()
}
]
}
}
I render the Starters components as follows:
...
<ScrollView
horizontal={true}
style={{
padding: 6,
paddingHorizontal: 10
}}
>
<Starters
item={itemData["Vits"]}
/>
<Starters
item={itemData["Vits2"]}
/>
</ScrollView>
...
As seen above, my handleAdd function sets a homeData const in a parent component by passing down the setHomeData function and calling it in the onPress of my Card‘s IconButton. showHomeModal in this function is also from the parent component and sets the value to true to display the modal.
This does two things:
- Set the
homeData
- Call the function to open a modal with
showHomeModal
The actual modal being called is supposed to display some of the data from itemData after the user presses the IconButton. I have some “filler” data to avoid any undefined errors in the modal like such:
const [homeData,setHomeData] = useState({
"ScheduleName": "Everyday",
"Stack": [
{
"amount": "3900",
"measure": "mg",
"substance": "Omega3",
id: "7846365hjbvbert674t"
},
{
"amount": "25",
"measure": "mg",
"substance": "B12",
id: "8r76438hjvfguy"
},
{
"amount": "2",
"measure": "Tablets",
"substance": "B-complex",
id: "783657345634hgvbhjvh"
},
],
"Name": "Example",
"Track": false,
"TrackString": "false",
"StartDate": new Date().toISOString()
});
My modal is passed the homeData and this data is rendered in the modal.
...
<Portal>
<HomeStarterModal
homeModalVisible={homeModalVisible}
homeData={homeData}
hideHomeModal={hideHomeModal}
/>
</Portal>
...
This is where my problem arises. When the modal opens, instead of showing me data of the current homeData, I’m getting the original “filler” data being displayed. For example:
const HomeStarterModal = ({
homeModalVisible,
homeData,
hideHomeModal
}) => {
const [formState,setFormState] = useState(homeData["Stack"]);
const [protocol,setProtocol] = useState(homeData["ScheduleName"]);
const [name,setName] = useState(homeData["Name"]);
return (
...
<View style={MainStyles.col}>
<Subheading>1. Give it a name</Subheading>
<TextInput
label="Name"
value={name}
onChangeText={name => setName(name)}
mode="outlined"
/>
</View>
...
This is returning an input with “Example” as opposed to what homeData‘s Name prop should be. When I log what the homeData is from the modal by doing something like this:
const HomeStarterModal = ({
homeModalVisible,
homeData,
hideHomeModal
}) => {
const [formState,setFormState] = useState(homeData["Stack"]);
const [protocol,setProtocol] = useState(homeData["ScheduleName"]);
const [name,setName] = useState(homeData["Name"]);
return (
...
<View style={MainStyles.col}>
<Subheading>1. Give it a name</Subheading>
<TextInput
label="Name"
value={name}
onChangeText={name => setName(name)}
mode="outlined"
/>
<Button
onPress={() => console.log(homeData)}
>
log
</Button>
</View>
...
This log output is the correct homeData object that corresponds to the correct Starters data that the user selects. So my question is this: How can I get the data to be correctly displayed in my modal? Why are useState hooks in my modal not detecting the change from the homeData variable being passed to my modal?