简体   繁体   中英

Pass state from component to Modal?

I have a component and import it in specific screen, in this screen i have a button when clicks i open modal contain component it's a "recorder" so after i record voices i want to take this voice and save them into Parent screen as a state or something!

in the recorder component, I save voices data into state? but how can i pass it to other parent screens?? so how can I handle it?

here is shots

Parent Screen "after click add voice I show the modal"

Parent Screen

Here's a modal contain a recorder component

Modal

CODE

Component

" I pass data to PassDataToModal state inside componentDidMount "

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import {AudioRecorder, AudioUtils} from 'react-native-audio';
import Sound from 'react-native-sound';
import Icon from 'react-native-vector-icons/MaterialIcons';

class RecorderScreen extends Component {
  state = {
    PassDataToModal: null,
  };



  componentDidMount() {
    AudioRecorder.requestAuthorization().then(isAuthorised => {
      this.setState({hasPermission: isAuthorised});

      AudioRecorder.onFinished = data => {
        console.log('data', JSON.stringify(data));
        this.setState({PassDataToModal: data});

      };
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <View style={styles.controls}>
          {this._renderPlayButton(() => {
            this._play();
          })}
          {this._renderRecordButton(this.state.recording)}
          {this._renderStopButton('Stop', () => {
            this._stop().then(() => this.setState({currentTime: 0}));
          })}
        </View>
        <Text style={styles.progressText}>{this.state.currentTime}s</Text>
      </View>
    );
  }
}

export default RecorderScreen;

Parent Screen

import Modal from 'react-native-modal';
import RecorderScreen from './Recorder';

class Order extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isModalVisible: false,
    };
  }

  toggleModal = () => {
    this.setState({isModalVisible: !this.state.isModalVisible});
  };
 render() {
    return (
      <View style={styles.container}>
          <TouchableOpacity
            onPress={this.toggleModal}
           >
            <Icon name="mic" color="#333" size={20} />
            <Text style={{paddingHorizontal: 5}}>Add Voice</Text>
          </TouchableOpacity>
         <Modal
           style={{margin: 0}}
           isVisible={this.state.isModalVisible}
         >
           <View>
              <TouchableOpacity onPress={this.toggleModal}>
                <Icon name="close" color="#000" size={25} />
              </TouchableOpacity>

              <RecorderScreen /> // Component

            </View>
        </View>
     )
  }
}

In your parent component pass a function to your RecorderScreen component that will send the necessary data up. Docs on lifting state up .

So in your parent you'd have something like:

setData = (data) => {
  // Set this to whatever you need it to be named
  this.setState({childData: data});
}

Then pass the function as a prop:

<RecorderScreen setData={this.setData} />

And finally, call it in the child however needed (If I'm following the code something like this):

 componentDidMount() {
    AudioRecorder.requestAuthorization().then(isAuthorised => {
      this.setState({hasPermission: isAuthorised});
      AudioRecorder.onFinished = data => {
        this.props.setData(data);
      };
    });
  }

Then your parent component will have access to the child's data that you have lifted up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM