Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass input Field Data from Modal to the Container in react-Native?

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.

like image 877
Abhishek Avatar asked May 04 '17 06:05

Abhishek


People also ask

Can we pass data from child to parent in react-native?

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 .

How do you pass data to child component react-native?

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!

How to pass data from modal to react app using NPX?

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

What is the use of react-modal?

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.

How to open a react bootstrap modal on the page?

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.

How to use data in iconmodal with function component?

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.


1 Answers

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.

like image 176
eden Avatar answered Oct 05 '22 02:10

eden