Skip to content

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

@Rmttf

Description

@Rmttf

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions