子组件更改其状态后执行回调

时间:2020-07-22 16:05:29

标签: javascript reactjs

我有一个类组件,该类组件呈现无法修改的子组件(来自库)。

该组件处于内部状态,在该状态下,我想检索数据并对其进行更改。

我尝试在ref上使用回调,如下所示:

onRefChange = el => {
  console.log('el state', el.state);
  // prints the initial state of the child component, with the value still to be changed (it changes a little after initialization)
}

<Carousel
  ref={(el) => {
    this.onRefChange(el);
    return this.Carousel = el;
  }}
  //other stuff
/>

我当然可以稍后读取this.Carousel.state来访问状态,但是我试图在此数据更改后立即通过回调进行操作。

除了更改子组件的行为外,父组件应仍为类componente,因此不能使用诸如useEffect()之类的钩子。

是死路一条还是有办法实现?

0 个答案:

没有答案