简体   繁体   English

react-spring:过渡组件问题

[英]react-spring : Transition component issue

I am using react-spring, Transition render prop component.我正在使用 react-spring,Transition 渲染道具组件。

I am facing an edge case where even after the items are updated via setState() the items used in component shows the old values and appends new values to it.我面临一个边缘情况,即使在通过 setState() 更新项目之后,组件中使用的项目也会显示旧值并向其附加新值。

I was expecting a behaviour where the items should re-render the list of items in Transition component.我期待一种行为,其中项目应该重新呈现 Transition 组件中的项目列表。

any idea what I might be missing?知道我可能会错过什么吗?

<ul>
  <Transition
    items={randomFeeds}
    // keys={randomFeeds.map(item  => item.id)}
    keys={randomFeeds => randomFeeds.id}
    from={{ transform: 'translate3d(0, -40px, 0)', opacity: 0 }}
    enter={{ transform: 'translate3d(0,0,0)', opacity: 1 }}
    update={{ transform: 'translate3d(0,0,0)', opacity: 1 }}
    reset={true}
    unique={true}
  >
    {item => props => (
      <li style={props} key={item.id + item.riskCategoryId + item.riskCategory}>
        <RimeCard
          key={item.id + item.riskCategoryId + item.riskCategory}
          title={item.title}
          categoryId={item.riskCategoryId}
          category={item.riskCategory}
          link={item.link}
          isTimeline={true}
          date={item.publishDateString}
        />
      </li>
    )}
  </Transition>
</ul>

If you want an item update then make sure that, the key of your li component is not changed between updates.如果您想要项目更新,请确保您的 li 组件的键在更新之间不会更改。 So instead of using key: key={item.id + item.riskCategoryId + item.riskCategory} just use key={item.id}因此,不要使用密钥: key={item.id + item.riskCategoryId + item.riskCategory}只需使用key={item.id}

And you may also want to add the leave property to your Transition for example: leave={{ transform: 'translate3d(0, 40px, 0)', opacity: 0 }}您可能还想将 leave 属性添加到您的 Transition 中,例如: leave={{ transform: 'translate3d(0, 40px, 0)', opacity: 0 }}

And if this solve your problem I'm not sure if reaset and unique is neccessary.如果这解决了您的问题,我不确定是否需要重新设置和独特。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM