简体   繁体   English

在React中消除jsx元素的渲染

[英]Debounce jsx element's rendering in React

I am wondering if it is possible to debounce a jsx element's rendering. 我想知道是否可以对jsx元素的呈现进行去抖动。 I have an animation of a panel expanding that has some content in it. 我有一个面板扩展的动画,其中包含一些内容。 If the panel is empty (just the background) the animation is smooth and works as expected. 如果面板为空(仅背景),则动画是平滑的,并且按预期工作。 The animation and associated components are all from Material-UI. 动画和相关组件均来自Material-UI。 The issue arises when the content is in the panel already so the animation (width expansion) just skips out to the width of the content making the animation look choppy. 当内容已经在面板中时,就会出现问题,因此动画(宽度扩展)只是跳到内容的宽度,使动画看起来不稳定。 Here is a similar example to what I am referring to. 这是一个与我所指的相似的例子 This code is my code I am using in my panel and they work the same in terms of the expansion. 该代码是我在面板中使用的代码,它们在扩展方面的作用相同。 Only difference is the content in this example is just lorem ipsum so the animation appears to work fine. 唯一不同的是,此示例中的内容只是lorem ipsum,因此动画看起来可以正常工作。 Is it possible to debounce the <CardContent /> component's rendering like this? 是否可以像这样对<CardContent />组件的呈现进行去抖动?

{ open &&
  (
    _.debounce(e => {
      return (
        <CardContent>
          {/*content in here*/}
        </CardContent>
      )
    }, 300)
  )
}

or something similar (this doesn't work) so the panel will be fully expanded through the animation before the content is rendered? 或类似的操作(这不起作用),以便在呈现内容之前通过动画完全扩展面板?

I'd like to add a comment however my reputation isn't above 50 yet so unfortunately I can't :p. 我想添加一条评论,但是我的声誉尚未高于50,所以我不能:p。

JS Animation JS动画

To answer your question, if you are using JavaScript animations like GSAP or AnimeJS you can have a state flag called. 要回答您的问题,如果您使用的是GSAPAnimeJS类的JavaScript动画,则可以设置一个状态标志。 isAnimating = null . isAnimating = null Then when your animation starts set the flag to true and when it's finished set it to false . 然后,当动画开始时,将标志设置为true ,完成时将其设置为false Then in your render() method write a conditional render like this.state.isAnimating === false && <CardContent /> . 然后在您的render()方法中编写一个条件渲染,例如this.state.isAnimating === false && <CardContent />

CSS Animation CSS动画

If you are using CSS animation you can use the transitionend event to wait for the animation to end. 如果您使用CSS动画,则可以使用transitionend事件来等待动画结束。 Therefore you have to create a ref on the animating DOM element to which you can attach the event. 因此,您必须在可以将事件附加到的动画DOM元素上创建一个ref

Does that help you a bit? 这对您有帮助吗? Please let me know if you have any other questions. 如果您还有其他问题,请告诉我。

Here is a solution using react spring library 这是使用React Spring库解决方案

  • I added a state variable in order to display or not the card content 我添加了一个状态变量以显示或不显示卡的内容

     const [contentVisible, setContentVisible] = React.useState(false); 
  • I created a spring to handle the width transition, I set contentVisible to true as soon as the width approaches the 300 我创建了一个弹簧来处理宽度过渡,当宽度接近300时,我将contentVisible设置为true

     const { width } = useSpring({ width: open ? 300 : 120, onFrame: ({ width }) => setContentVisible(width > 299.8) }); 
  • Finally, I created an animated card component 最后,我创建了一个动画卡片组件

     const AnimatedCard = animated(Card); ... <AnimatedCard classes={{ root: classes.card }} style={{ width }}> 

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

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