简体   繁体   English

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

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

I'm trying to make a simple Pomodoro timer.我正在尝试制作一个简单的番茄钟计时器。 I need to make the pause and stop buttons work.我需要使暂停和停止按钮工作。 I defined a separate component called 'Timer' and added two buttons: 'Pause' and 'Stop' which obviously have to affect the state of the Timer.我定义了一个名为“Timer”的单独组件并添加了两个按钮:“Pause”和“Stop”,这显然必须影响 Timer 的状态。

How do I call the stop and pause method of Timer when the respective Buttons are pressed?按下相应按钮时如何调用 Timer 的停止和暂停方法?

I understand that I can do this by simply including the buttons within the Timer class but I want to learn how to achieve something similar in the future and I'd like to keep the counter part of the Timer independent.我知道我可以通过简单地在 Timer 类中包含按钮来做到这一点,但我想学习如何在未来实现类似的东西,我想保持 Timer 的计数器部分独立。

Here is the code:这是代码:

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',
  }
});

Well actually you can do that using ref feature.那么实际上你可以使用ref功能来做到这一点。

You can create a ref and assign it to your timer component.您可以创建一个ref并将其分配给您的计时器组件。 Then you can call this component's methods.然后你可以调用这个组件的方法。

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

But this doesn't seem like a react way of doing things.但这似乎不是一种反应式的做事方式。

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

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