简体   繁体   English

如何渲染相同的组件但具有初始状态(有条件地)?

[英]How to render the same component but with initial state (conditionally)?

I have one specific component that manages its own state using useState .我有一个特定的组件,它使用useState管理自己的状态。 The problem is that I want to render conditionally the same component:问题是我想有条件地渲染相同的组件:

const PaymentScannerView: React.FC<Props> = (props: React.PropsWithChildren<Props>) => {
  const { t } = useTranslation();

  return props.onFront ? (
    <PBScanner
      header={t('paymentScanner.front.capture.header')}
      instruction={t('paymentScanner.front.capture.instruction')}
      reCaptureText={t('paymentScanner.front.confirmation.reCaptureText')}
      continueText={t('paymentScanner.front.confirmation.continueText')}
      confirmCaptureHandler={props.confirmFrontCaptureHanlder}
      reCaptureHandler={props.reCaptureFrontHandler}
    />
  ): (
    <PBScanner
      header={t('paymentScanner.back.capture.header')}
      instruction={t('paymentScanner.back.capture.instruction')}
      reCaptureText={t('paymentScanner.back.confirmation.reCaptureText')}
      continueText={t('paymentScanner.back.confirmation.continueText')}
      confirmCaptureHandler={props.confirmBackCaptureHandler}
      reCaptureHandler={props.reCaptureBackHandler}
    />
  );
};

But I don't want to share this state between those 2 "versions".但我不想在这两个“版本”之间共享这种状态。 To clarify it, suppose this PBScanner component has internal state of counting from 0 .为了澄清它,假设这个PBScanner组件具有从0计数的内部状态。 Then is it renders for 5 times within the first condition (meaning that props.onFront is true 5 times), then, even if suddenly props.onFront would equal to false then the second version would preserve the state and would be rendered with counter equal to 5 .那么它是否在第一个条件下渲染了 5 次(意味着props.onFront为真 5 次),那么,即使突然props.onFront将等于false那么第二个版本将保留状态并以计数器相等的方式渲染到5

That's not what I want.那不是我想要的。 I want to "treat" these two components as they won't share the state.我想“处理”这两个组件,因为它们不会共享状态。

How can I do it?我该怎么做?

It depends on what you actually want.这取决于你真正想要什么。

  1. If the state should be maintained in both components individually, then you should render them both.如果应该在两个组件中单独维护状态,那么您应该同时渲染它们。 The component itself should then decide if it will return anything.然后组件本身应该决定它是否会返回任何东西。 (Per @NicolasMenettrier suggestion). (根据@NicolasMenetrier 的建议)。

  2. If you want them to lose their state, when props.onFront changes, then the simplest solution is to add a key-property.如果您希望它们在props.onFront更改时丢失其状态,那么最简单的解决方案是添加一个 key-property。 Eg:例如:

     <PBScanner key="front" .../> // and "back" respectively

    This way react will regard both as distinct, giving them their own state.这种方式 react 会将两者视为不同的,给它们自己的状态。
    Note though that this will make them lose their state everytime props.onFront changes.请注意,这将使它们在每次props.onFront更改时丢失其状态。

as They said previously, it depends on what you want.正如他们之前所说,这取决于您想要什么。 in my opinion the simple solution would be to add key property to PBScanner .在我看来,简单的解决方案是将key属性添加到PBScanner

an other solution is to add useEffect hook in **PBScanner ** like this :另一种解决方案是在 **PBScanner ** 中添加useEffect钩子,如下所示:

import React, {useEffect} from 'react'; 
...
export default PBScanner({header, ...}) {
const [count, setCount] = useState(0);
UseEffect(() => {
setCount(0); // each time PBScanner update with change in header
// (instruction...), Count go to 0.
}, [header]);//here you can use header or instruction ...

}

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

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