简体   繁体   English

ReactJS:渲染组件时触发方法

[英]ReactJS: Fire Method when Component is rendered

I got a general question about ReactJS: 我对ReactJS有一个一般性的问题:

Is it possible to fire a method as soon as all the JSX-Components ( <div> components </div> ) are successfully rendered? 一旦成功渲染所有JSX-Components( <div> components </div> ),是否有可能触发一种方法? So if some are only rendered on certain conditions could i fire a method as soon as all of them are true . 因此,如果仅在某些条件下渲染某些对象,那么我可以在所有这些条件都true立即触发一种方法。

How would i check if everything got sucessfully rendered? 我如何检查所有内容是否都成功渲染?

There is a lifecycle method componentDidMount that will get called immediately after react renders the component, it will get called once, only after the initial rendering, i think you can use that method. 有一个生命周期方法componentDidMount会在react渲染组件后立即调用,它将被调用一次,只有在初始渲染之后,我认为您可以使用该方法。

As per DOC : 根据DOC

componentDidMount() is invoked immediately after a component is mounted. 挂载组件后立即调用componentDidMount()。 Initialization that requires DOM nodes should go here. 需要DOM节点的初始化应在此处进行。 If you need to load data from a remote endpoint, this is a good place to instantiate the network request. 如果需要从远程端点加载数据,这是实例化网络请求的好地方。 Setting state in this method will trigger a re-rendering. 在此方法中设置状态将触发重新渲染。

About second ques: 关于第二个问题:

You are rendering the elements on the basis of some conditions, i am assuming that you are using some kind of bool or checking the length of array, so there is a lifecycle method componentWillUpdate , it will get called before the react re-render the component (it will not get called on initial rendering) inside this method you can check for all conditions and call any method if all the conditions are true. 您正在根据某些条件渲染元素,我假设您正在使用某种布尔值或检查数组的长度,因此有一个生命周期方法componentWillUpdate ,它将在react重新渲染组件之前被调用(在初始渲染时不会调用)此方法内,您可以检查所有条件,并在所有条件都为真时调用任何方法。 So whenever you do setState it will get called and you can put the logic here. 因此,无论何时执行setState都会被调用,您可以在此处放置逻辑。

This is just a skeleton, but it should help you to get to the solution that you're looking for: 这只是一个框架,但是它应该可以帮助您获得所需的解决方案:

type ChildProps = {
    onUpdated: () => void;
}

class MyChildComponent extends React.component<ChildProps, void> {
    componentDidUpdate() {
        this.props.onUpdated();
    }

    render() {
        ...
    }
}

class MyComponent extends React.Component<void, void> {
    private childCount: number;
    private updated: number;

    constructor() {
        this.childCount = 2;
    }

    render() {
        this.updated = 0;

        return (
            <div>
                <MyChildComponent onUpdated={ this.childUpdated.bind(this) } />
                <MyChildComponent onUpdated={ this.childUpdated.bind(this) } />
            </div>
        );
    }

    private childUpdated() {
        this.updated++;
        if (this.updated === this.childCount) {
            // do something...
        }
    }
}

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

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