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

fix(useDrag): 增加一个参数,可以设置在getProps方法返回的对象中不默认添加key属性 #1004

Merged
merged 3 commits into from
Jul 1, 2021

Conversation

xlkang
Copy link
Contributor

@xlkang xlkang commented Jun 25, 2021

给useDrag增加一个参数,允许设置在getProps方法返回的对象中不默认添加key属性。

考虑以下useDrag不直接用于列表循环而被包装后使用的例子:

function DraggableBox({ data, children, ...rest }) {
  const getProps = useDrag({
    onDragStart: (_data) => {
      // somecode
    },
    onDragEnd: (_data) => {
      // somecode
    },
  })

  return  (
    <li
      {...getProps(data)}
      {...rest}
    >
      {children}
    </li>
  )
}

getProps(data) 返回的对象默认包含了一个由 data 序列化得到 key字符串, 当 data 改变时,key随之改变,此时旧的li节点会被直接替换成新的li节点,而children 也被重新 mount 而不是期望的 update

一个children被mount的例子

@CLAassistant
Copy link

CLAassistant commented Jun 25, 2021

CLA assistant check
All committers have signed the CLA.

@brickspert brickspert requested a review from ttys026 June 26, 2021 03:05
@xlkang xlkang changed the title Xlkang fix use drag fix(useDrag): 增加一个参数,可以设置在getProps方法返回的对象中不默认添加key属性 Jun 26, 2021
@ttys026
Copy link
Collaborator

ttys026 commented Jun 28, 2021

感谢贡献,建议 getPropsWithKey 可以放在 interface IConfig 里,作为 property。
这样参数比较聚拢,写配置的时候代码提示也比较友好

@xlkang
Copy link
Contributor Author

xlkang commented Jun 28, 2021

感谢贡献,建议 getPropsWithKey 可以放在 interface IConfig 里,作为 property。
这样参数比较聚拢,写配置的时候代码提示也比较友好

感谢建议,最初的考虑确实有不妥,已做相应调整。

@brickspert brickspert merged commit 00696db into alibaba:master Jul 1, 2021
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

Successfully merging this pull request may close these issues.

4 participants