简体   繁体   English

父状态更新后如何只调用一次子组件中的方法

[英]How to call method in child component only once after parent state update

I have one parent and one child.我有一个父母和一个孩子。 The parent retrieves data (data is named blurts) using a fetch request, and sends the data via props to the child component.父组件使用 fetch 请求检索数据(数据被命名为 blurts),并通过 props 将数据发送到子组件。 The child component is a lazy-loader component that only displays 6 records at a time as the user scrolls.子组件是一个惰性加载器组件,在用户滚动时一次只显示 6 条记录。

The lazy component creates its own state (array) based on the number of parent component records, which determines whether a record should be visible.惰性组件根据父组件记录的数量创建自己的状态(数组),这决定了记录是否应该可见。 All isVisible properties are set to false initially.所有 isVisible 属性最初都设置为 false。

componentDidMount() in lazy component:惰性组件中的 componentDidMount():

componentDidMount() {
    var isVisibleArray = this.props.blurts.map(item => {
        return false;
    });
    this.setState({ isVisible: isVisibleArray });
    this.PushBlurtArray();
    window.addEventListener('scroll', this.LazyLoader);
}

Then, PushBlurtArray() method is called in order to set the first 6 records to visible.然后,调用 PushBlurtArray() 方法以将前 6 条记录设置为可见。 This method is also called each time the user scrolls to the sixth record.每次用户滚动到第六条记录时也会调用此方法。 Internal state in the lazy-loader keeps track of this and sets 6 more to visible as needed.延迟加载器中的内部状态会对此进行跟踪,并根据需要将另外 6 个设置为可见。

PushBlurtArray() in lazy component:惰性组件中的 PushBlurtArray():

PushBlurtArray() {
    // Initial values that keep track of lazy state.
    var renderCount = this.state.lazyState.skipCount,
        numRendered = this.state.numberRendered,
        isVisibleUpdate = this.state.isVisible;

        // Logic to get the next 6 (or less) blurts in the array and set isVisible to true
        // each time this method is called by the lazyloader method.
        for(let i=numRendered; i<renderCount; i++) {
            if (this.props.blurts[i]) {
                isVisibleUpdate[i] = true;
                numRendered++;
            }
        }

        // Update state accordingly so the loop iterates over the proper numerical
        // sequence keeping the blurts in order of consecutive sixes.
        this.setState(prevState => ({
            lazyState: {
                lzyLoading: true,
                skipCount: prevState.lazyState.skipCount + prevState.lazyState.skipInc,
                skipInc: 6,
                skipDb: prevState.lazyState.skipDb
            },
            isVisible: isVisibleUpdate,
            numberRendered: numRendered
        }));
}

This works properly up to a point.这在一定程度上可以正常工作。 The restraint I'm having trouble with is that the parent component only pulls an initial count of 42 records.我遇到的限制是父组件只提取 42 条记录的初始计数。 Then, once the lazy loader has iterated through all 42, I call a method in the parent that mounts another 42 records to its state, which in turn re-renders the lazy-loader.然后,一旦惰性加载器遍历了所有 42 条记录,我就会调用父级中的一个方法,该方法将另外 42 条记录安装到其状态,进而重新呈现惰性加载器。 In theory this should go on indefinitely without issue.从理论上讲,这应该无限期地继续下去而不会出现问题。

The problem I'm having is that PushBlurtArray() needs to get called once the 42 additional records has been added to the parent state.我遇到的问题是,一旦将 42 条附加记录添加到父状态,就需要调用 PushBlurtArray()。 If I call PushBlurtArray() in the render() method, or in componentDidUpdate(), it ends up calling PushBlurtArray() repetitively which pretty much screws up the entire ordeal.如果我在 render() 方法或 componentDidUpdate() 中调用 PushBlurtArray(),它最终会重复调用 PushBlurtArray(),这几乎搞砸了整个过程。

I need a way to call PushBlurtArray() just once, after the 42 records has been added to state in the parent component.在将 42 条记录添加到父组件中的 state 之后,我需要一种方法来调用 PushBlurtArray() 一次。

You can check the transition of your data inside componentDidUpdate .您可以在componentDidUpdate检查数据的转换。 In this instance you want to check for the length of your array to determine when it changes.在这种情况下,您要检查数组的长度以确定它何时发生变化。

componentDidUpdate(props) {
  if (
    (this.props.blurts.length > 0 && !props.blurts.length) ||
    this.props.blurts.length !== props.blurts.length
  ) {
    this.PushBlurtArray();
  }
}

暂无
暂无

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

相关问题 仅在订阅 httpClient observables 后,如何从父组件方法调用子组件方法? - How to call child component method from parent component method only after subscribing httpClient observables? 子组件更新服务器后如何更新父组件的state? - How to update state of parent component after the server is updated in child component? 如何在父事件发生后调用子(或任何其他)组件中的方法? - How to call a method in a child (or any other) component after an event in the parent? React:更改子组件后如何正确更新父组件的 state - React: How to properly update state of parent component after changing child component 如何从Reactjs中的子组件更新父组件的状态 - How to update a parent's component state from a child component in Reactjs 只有在父组件完成重新渲染后才调用子组件中的方法 - Call a method in child component only after parent completes re-render 如何一次(同时)更新父 state 和子组件道具 - How to update parent state and child components props at once (at the same time) 如何从子级调用父级的函数而不更新父级状态? - How to call function of a parent from child and do not update the parent state? 从子组件更新父组件状态 - Update Parent Component State from Child Component Reactjs:如何访问子组件的 state。从依赖于子组件 state 的父组件中的方法 - Reactjs: How to access state of child component.from a method in parent component that depends on state of child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM