【问题标题】:Access State and Methods from Parent Class within Child Class in ReactJSReactJS中子类中父类的访问状态和方法
【发布时间】:2017-05-11 18:17:28
【问题描述】:

我在 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>;
    }

}

在另一个类中,我正在初始化一个子对象并在其上调用函数 startManager()。但随后我收到一条错误消息,提示 无法读取未定义的属性“开始”。我是 React 的新手,我认为我在这里定义孩子的属性时做错了。有谁知道我在这里做错了什么。谢谢

【问题讨论】:

  • 你是如何“初始化一个子对象”的?
  • 在另一个类的构造函数中。像构造函数 () { super(); this.zeitmanager = new Zeitmanager() } 然后我调用 zm 对象上的函数。像 zm.startManager()

标签: javascript reactjs typescript


【解决方案1】:

你不能像那样实例化你的组件,你需要 React 为你做这件事,只有这样整个 props/state 的东西才能工作。

你可以使用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 } />
      )
   }
}

【讨论】:

  • 谢谢,我将如何使用另一个类的这个参考?
  • 取决于类之间的交互对象以及您的整体设计。我刚刚添加了useZeitManager 函数,但是您可以使用现有实例的方法来代替
猜你喜欢
  • 2012-02-04
  • 2017-09-22
  • 2014-10-04
  • 1970-01-01
  • 2023-02-04
  • 2017-07-15
  • 2020-06-26
  • 1970-01-01
相关资源
最近更新 更多