当前位置: 首页 > 知识库问答 >
问题:

从ReactJS子类中的父类访问状态和方法

乐正峰
2023-03-14

我在ReactJS中得到了一个子类和一个父类。我想让我的孩子访问父级的状态,这是每毫秒更新一次。孩子也应该有可能接触到一些父母的方法。

interface StatesZeit{
        datum : Date;
    }

    export default class StoppUhr extends React.Component<undefined, StatesZeit>{
        uhrID : number;

        constructor(){
            super();
            this.state = {
                datum : new Date(0),
            };
            this.start = this.start.bind(this);
            this.stopp = this.stopp.bind(this);
            this.counter = this.counter.bind(this);
        }

        start() {
            this.uhrID = setInterval(() => {
                this.counter();
            }, 0);
        }

        stopp() {
            clearInterval(this.uhrID);
        }

        counter() {
            this.setState({
                datum : new Date()
            });
        }

        render(){ // Returns the Child and passes the states as properties
            return (
                <ZeitManager stopp={this.stopp.bind(this)}
                             start={this.start.bind(this)}
                             counter={this.counter.bind(this)}
                             timeState={this.state.datum}/>
            )
        }
    }
export interface PropsZM {
    timeState : Date;
    start(); // not sure if correct, should somehow reference functions of parent
    stopp();// not sure if correct, should somehow reference functions of parent
    counter();// not sure if correct, should somehow reference functions of parent
}

export class ZeitManager extends React.Component<PropsZM, undefined>{
    private _millisec : number;

    constructor(){
        super();
        this.startManager = this.startManager.bind(this);
        this.stoppManager = this.stoppManager.bind(this);
    }

    startManager() {
        this.props.start();
    }

    stoppManager() {
        this.props.stopp();
        this._millisec = this.props.timeState.getMilliseconds();
    }

    counterManager() {
        this.props.counter;
    }

    get millisec(): number {
        return this._millisec;
    }

    render(){
        return <div>{this.props.start}</div>;
    }

}

在另一个类中,我正在初始化一个子对象并对其调用Function startManager()。但随后我收到一条错误消息,说无法读取未定义的属性“start”。我是一个新的反应者,我认为我在定义孩子的属性时犯了一些错误。有人知道我做错了什么吗。谢谢

共有1个答案

堵存
2023-03-14

你不能像那样实例化你的组件,你需要为你做出反应,只有这样,整个道具/状态的事情才会起作用。

您可以使用React.createElement:

zm = React.createElement(ZeitManager);

或获取对实例的引用:

function useZeitManager(instance: ZeitManager) {
    // do what ever with it
}

export default class StoppUhr extends React.Component<undefined, StatesZeit>{
    ...

   render() {
      return (
         <ZeitManager 
            ref={ el => useZeitManager(el) }
            stopp={ this.stopp.bind(this) }
            start={ this.start.bind(this) }
            counter={ this.counter.bind(this) }
            timeState={ this.state.datum } />
      )
   }
}
 类似资料:
  • 我试图了解如何构建具有不同"页面"或"视图"的ReactJS应用程序。 我将以下组件作为我的基础应用程序,并且我正在React状态中使用currentState属性在视图中的活动组件之间切换。 它执行此任务,但在启动dataLoaded回调时,组件从不接收更新的recipes数组。 如何根据应用程序中的更新状态更新道具? 还是我处理这整件事的方式错了?

  • 为什么我们不能在子类B中声明一个实例方法,它与父类a中的静态方法共享相同的签名? 在上面的代码中,由于A没有该名称的实例方法,并且由于Java允许对象访问静态方法,所以指向B的类型为A的对象A可以调用它中存在的静态方法(类A)。但是编译器抛出一个错误“实例方法不能重写静态方法”为什么? 注意:我可以理解,如果一个类不允许两个方法使用相同的方法名,即使其中一个是实例,另一个是静态的。但是我不明白为什

  • 问题内容: 得知子类的类变量无法访问父类的类变量而没有特别指出父类的名称,我感到很惊讶: 为什么在定义By时我必须引用Ax,而不仅仅是x?这与我对实例变量的直觉是相反的,并且因为在定义B之后我可以引用Bx。 问题答案: 在Python中,在创建类之前,将在其自己的名称空间中执行类的主体(此后,该名称空间的成员将成为该类的成员)。因此,当解释器达到y = x + 1时,此时B类尚不存在,因此没有父类

  • 我有以下情况: 在执行代码时,输出为:5。 如何通过父类方法访问子类(B)变量(x)? 这是否可以在不重写print()方法的情况下完成(即在B中取消注释)? [这很重要,因为在重写时,我们将不得不再次重写print()方法的整个代码] 编辑过 进一步澄清:- 问题的动机是使用父类方法中的子类私有变量的值。这不需要更改父类私有变量的值来获得所需的结果 (感谢大家的时间和帮助)

  • 事实上,我正在学习如何反应JS,但我不清楚家长的类方法如何能够访问孩子的类状态。我在这个主题上搜索了很多,但在面向对象编程中,父类无法访问其子类的属性(状态)。但在react js中,setState()如何访问其子级的类属性(state)。这可能是个愚蠢的问题请告诉我它是怎么发生的?