vuereact-combined icon indicating copy to clipboard operation
vuereact-combined copied to clipboard

解决vue接入react时useImperativeHandle无法获取到最新的状态

Open Rmttf opened this issue 11 months ago • 0 comments

我目前的解决方案:通过先在当前react组件外包裹一个新的react,在新react组件中处理ref更新问题,然后vue接入新的组件。通过这种方式保证vue接入react组件不需要处理ref。

// 子组件(react):
useImperativeHandle(
    ref,
    () => ({
        query({ selectedWays, rangeTime }) {
            // ❌异常:当direction, stage在子组件内部变更时,父组件(vue)调用该query拿到的还是初始化的状态
            console.log(direction, stage)
        },
    }),
    [direction, stage]
);

// 父组件(vue):
<div>
      <ChildComp ref="childRef" />
</div>

handleQuery(curRangeTime, selected) {
    if (this.$refs.doorRef) {
        this.$refs.childRef.query({ selectedWays: [...selected], rangeTime: curRangeTime });
    }
},

Originally posted by @Rmttf in https://github.com/gloriasoft/vuereact-combined/issues/32#issuecomment-2550489464

Rmttf avatar Dec 18 '24 11:12 Rmttf