繁体   English   中英

从另一个组件调用 React 组件的特定实例上的方法

[英]Calling a method on a particular instance of a React component from another component

我正在尝试制作一个简单的番茄钟计时器。 我需要使暂停和停止按钮工作。 我定义了一个名为“Timer”的单独组件并添加了两个按钮:“Pause”和“Stop”,这显然必须影响 Timer 的状态。

按下相应按钮时如何调用 Timer 的停止和暂停方法?

我知道我可以通过简单地在 Timer 类中包含按钮来做到这一点,但我想学习如何在未来实现类似的东西,我想保持 Timer 的计数器部分独立。

这是代码:

import React from 'react'
import { Text, View, Button, StyleSheet } from 'react-native';
import { Constants } from 'expo';

class Timer extends React.Component{
  constructor (props){
    super(props)
    this.state = {
      minutes: props.minutes,
      seconds: props.seconds,
      count: 0,
    }
  }

  dec = () => {
      this.setState(previousState => {
        if (previousState.seconds==0){
          return {
            minutes: previousState.minutes-1,
            seconds: 59,
            count: previousState.count+1,
          }
        }
        else{
          return{
            minutes: previousState.minutes,
            seconds: previousState.seconds-1,
            count: previousState.count+1,
          }
        }
      });
  }
  componentDidMount (){
    setInterval(this.dec,1000);
  }

  render (){
    return(
      <View style = {{flexDirection: 'row'}}>
        <Text style = {styles.timerCount}> {this.state.minutes}</Text>
        <Text style = {styles.timerCount}>:</Text>
        <Text style = {styles.timerCount}> {this.state.seconds} </Text>
      </View>
      )
  }
   stop (){
    console.log('stop')
  }

  pause (){
   console.log('pause')
 }
}

export default class App extends React.Component {
  stop (){
    console.log('stop')
  }
  render() {
    return(
      <View style={styles.container}>
        <Timer style = {styles.timer} minutes={25} seconds = {0}/>
        <View style = {styles.buttonContainer}>
          <Button title = 'Pause' style = {styles.timerButton} color = 'white' onPress = {()=>{console.log("call the timer's pause method here")}}/>
          <Button title = 'Stop' style = {styles.timerButton} color = 'white' onPress = {()=>{console.log("call the timer's stop method here")}}/>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  buttonContainer: {
    flexDirection: 'row',
  },

  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: 50,
    backgroundColor: '#EC3D40',
  },

  timer: {
    backgroundColor: '#EC3D40',
    paddingTop: 50,
  },

  timerCount: {
    fontSize: 48,
    color: 'white',
    backgroundColor: '#EC3D40',
    paddingTop: 10,
    paddingBottom: 10,
  },

  timerButton:{
    borderColor: 'white',
    backgroundColor: 'transparent',
  }
});

那么实际上你可以使用ref功能来做到这一点。

您可以创建一个ref并将其分配给您的计时器组件。 然后你可以调用这个组件的方法。

export default class App extends React.Component {
  timerRef = React.createRef();

  render() {
    return(
      <View style={styles.container}>
        <Timer style = {styles.timer} minutes={25} seconds = {0} ref={this.timerRef}/>
        <View style = {styles.buttonContainer}>
          <Button title = 'Pause' style = {styles.timerButton} color = 'white' onPress = {()=>{console.log("call the timer's pause method here"); this.timerRef.current.pause();}}/>
          <Button title = 'Stop' style = {styles.timerButton} color = 'white' onPress = {()=>{console.log("call the timer's stop method here"); this.timerRef.current.stop();}}/>
        </View>
      </View>
    );
  }
}

但这似乎不是一种反应式的做事方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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