简体   繁体   English

如何防止动画后的组件渲染

[英]How to prevent component from rendering after animation in react

I have got a component which displays a hint only a single time to each user. 我有一个仅向每个用户显示提示的组件。 Lets call it <Hint /> . 让我们称之为<Hint />

While rendering the component, I am looking for a confirmed: boolean . 在渲染组件时,我正在寻找一个confirmed: boolean If its false , the component should render. 如果为false ,则组件应呈现。 If true , the component should not render at all. 如果为true ,则该组件完全不应渲染。

Thats done easily using 使用即可轻松完成

function Hint(confirmed: boolean) {
   if(confirmed) return null;
   return (
      <Example>
          <Text>I am a super helpful hint.</Text>
          <Button title="Okay" onClick={this.hideHint} />
      </Example>
}

But turns out, my <Example /> has a beautiful fade-out animation I want to keep and display. 但是事实证明,我的<Example />有一个漂亮的淡出动画,我想保留并显示。

How can I tell my <Hint /> component to not render at all if confirmed === true , but keep it if it was false and gets true , to keep the animation? 如果confirmed === true ,如何告诉<Hint />组件根本不渲染,但是如果它为false并变为true ,则保留它以保持动画?

Can I use something like ComponentDidMount() ? 我可以使用类似ComponentDidMount()东西吗? I feel very insecure using the lifecycle methods from react native. 我使用React Native的生命周期方法感到非常不安全。

Hint is a stateless Components , it'll be re-rendered each time his parent renders. 提示是一个无状态的Components ,它将在他的父母每次渲染时重新渲染。

If you want to avoid multiple re-renders you can implement a stateful component , that'll only re-render when you change the props or the state of the component. 如果要避免多次重新渲染,可以实现一个有状态的组件 ,只有在更改道具或组件的状态时才重新渲染。

For even more control on the re-render, you can implement the shouldComponentUpdate method in your statefull component. 为了对重新渲染进行更多控制,可以在statefull组件中实现shouldComponentUpdate方法。

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

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