Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Rmttf opened this issue Dec 18, 2024 · 0 comments
Open

Comments

@Rmttf
Copy link

Rmttf commented Dec 18, 2024

我目前的解决方案:通过先在当前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 #32 (comment)

@Rmttf Rmttf changed the title 我也遇到了相同的情况: 解决vue接入react时useImperativeHandle无法获取到最新的状态 Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant