I have these two components one is a modal and the other the stack navigator. so in the stack navigator im defining a function that will toggle the modal, but the function is not being passed and dont know the reason why,
//this is a stack navigator
export default function BudgetNavigator() {
const [isModalVisible, setIsModalVisible] = useState(false);
const toggleModal = () => {
setIsModalVisible(!isModalVisible);
};
//this how im passing it to the modal
<Stack.Screen
name="BudgetDetails"
component={BudgetDetails}
options={{
title: "Budget Details",
headerRight: () => (
<SimpleLineIcons
name="options-vertical"
size={24}
color="black"
style={{ marginRight: 10 }}
onPress={toggleModal}
/>
),
}}
/>
{isModalVisible && (
<Stack.Group screenOptions={{ presentation: 'modal' }}>
<Stack.Screen
name="OptionsModal"
component={OptionsModal}
options={{ headerShown: false }}
initialParams={{ isModalVisible, toggleModal }}
/>
</Stack.Group>
)}
//and this the modal, and how im access the function
import React from 'react';
import { View, Text, Button, Modal } from 'react-native';
const OptionsModal = ({ isModalVisible, toggleModal }) => {
console.log("modal isModalVisible",isModalVisible);
console.log("modal toggle",toggleModal);
return (
<Modal
visible={isModalVisible}
animationType="slide"
transparent={true}
>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'rgba(0, 0, 0, 0.5)' }}>
<View style={{ backgroundColor: 'white', padding: 20, borderRadius: 10 }}>
<Text>Option Modal</Text>
<Button title="Close" onPress={toggleModal} />
</View>
</View>
</Modal>
);
};
export default OptionsModal;
i was trying to pass the function defined in the parent component and passed to the child, but it seems the function ain’t being passed
New contributor
Willywipeout is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.