【问题标题】:Pass state from component to Modal?将状态从组件传递到模态?
【发布时间】:2019-11-01 15:06:19
【问题描述】:

我有一个组件并将其导入特定屏幕,在这个屏幕上我有一个按钮,当点击时我打开模式包含组件它是一个“录音机”,所以在我录制声音后我想把这个声音保存到父屏幕中作为一个国家或其他东西!

在录音器组件中,我将语音数据保存到状态!但我怎样才能将它传递给其他父屏幕!? 那么我该如何处理呢?

这里有截图

父屏幕“点击添加语音后我显示模态”

Parent Screen

这是一个包含记录器组件的模态

Modal

代码

组件

" 我将数据传递给 componentDidMount 内部的 PassDataToModal 状态"

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;

父屏幕

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>
     )
  }
}

【问题讨论】:

  • 请尽量将您的帖子限制在说明您面临的问题所需的特定/最少代码量。我什至在这里都找不到模态组件
  • 谢谢,@ChrisB。你能再检查一遍吗?

标签: javascript reactjs react-native


【解决方案1】:

在您的父组件中,将一个函数传递给您的RecorderScreen 组件,该函数将向上发送必要的数据。 Docs on lifting state up

所以在你的父母中你会有类似的东西:

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

然后将函数作为道具传递:

<RecorderScreen setData={this.setData} />

最后,根据需要在孩子中调用它(如果我遵循这样的代码):

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

那么你的父组件就可以访问你提升的子组件的数据了。

【讨论】:

  • 太好了,现在只是为了理解,如果我有一个数据并且我想将它传递给父级,我只需在我的子组件中添加 this.props.whatever(data) 并在父级中添加 &lt;child whatever={this.whateverFuncInParent} /&gt;对吗?
  • 是的!我链接的文档可能比我能更好地解释它,因此如果您有几分钟时间阅读该页面可能会有所帮助。这是一个非常重要的反应模式,所以值得花一些时间来确保它有意义
  • 你好布赖恩,它应该是母校名称setData函数等于道具名称?
  • prop 名称不必与函数名称匹配,只要访问正确即可。有时在追踪道具的来源时会更清楚。但情况并非总是如此,因此命名道具取决于具体情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 2021-11-11
  • 1970-01-01
相关资源
最近更新 更多