Here is what I am using:
<Modal visible = {this.props.visible} animationType="slide" transparent onRequestClose={() => {}} > <TextInput style = {styles.inputBox} ref = {this.props.destinatinon} /> </Modal>
and in the Container
<ExampleModal
destination = {this.state.destination} >
</ExampleModal>
I don't know how to pass data from Modal to Parent Component. Any kind of Tutorial or link is fine. Thanks in Advance.
To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .
You can also pass events like onClick or OnChange Just call an alert method in the childToParent function and pass that function as a prop to the child component. And in the child component, accept the childToParent function as a prop. Then assign it to an onClick event on a button. That's it!
In a Modal, we can easily pass data and also fetch back any information from the modal itself. Let’s start… First, we’ll create a new React application using npx create-react-app command After creating the react application, now we’ll install the React Bootstrap package by running below command
The react-modal package module provides use reusable components to create modals very easily and quickly in a react application. We can easily add our own style and animation to the modal container. it provides a good number of event handlers to control data flow from/ to modal components.
It can be managed to be opened on the exact center of the page vertically by adding the centered property Size of the React Bootstrap modal can be changed by setting the size property. We can also add a custom class by using the dialogClassName="full-screen-modal" To open a fullscreen bootstrap modal on-page.
and you can use these data in IconModal as this.props.modalData. If there is more data then you can always add another prop. Define the following Hooks in your function Component. Now Trigger the function which opens the Modal, while simultaneously passing data into it. And finally a snippet of the Modal Code.
Let's assume that your Modal is filed separately in /components/MyModal
to generalize things.
You can make your Modal call a function that you passed by props every time input text is changed. Here's a simple callback logic you can use.
Avoid using refs as much as you can.
import MyModal from '../components/MyModal';
...
class Home extends Component {
onInputChanged = (changedText) => {
console.log('This is the changed text: ', changedText);
}
render() {
return (
<View>
...
<MyModal onInputChanged={this.onInputChanged} .../>
</View>
)
}
}
// components folder
class MyModal extends Component {
render() {
return (
<Modal
visible = {this.props.visible}
animationType="slide"
transparent
onRequestClose={() => {}} >
<TextInput
style = {styles.inputBox}
onChangeText={(changedText) => this.props.onInputChanged(changedText)} />
</Modal>
)
}
}
Side Note: You can define MyModal stateless to make things a bit cleaner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With