运行useEffect后未重新分配变量

问题描述 投票:1回答:1

所以我在开头定义了这两个元素

let interestElm = (
    <Row
      title={translate('calculator.interest')}
      value={translate('currency_amount', {
        amount: offer.totalInterest,
      })}
    />
  );
  let commisionElm = (
    <Row
      title={translate('calculator.commission')}
      value={translate('currency_amount', {
        amount: offer.newInitialCommission,
      })}
    />
  ); 

然后在渲染页面useEffect时运行此功能

useEffect(()=>{
    discountElemHandler(discountData)
  }, [discountData, amount])

此函数检查一些值,并且它们存在,应该重新分配变量

const discountElemHandler = (discount) => {
    if (discount.length) {
      discount.forEach(discountItem => {
        if (discountItem.type === 'Type1') {
          interestElm = (
            <Row
              inheritStyle={style.discount}
              title={translate('calculator.interest')}
              discount={translate('currency_amount', {
                amount: offer.newInterest + discountItem.amount,
              })}
              value={translate('currency_amount', {
                amount: offer.totalInterest,
              })}
            />
          );
        }

        if (discountItem.type === 'Type2') {
          commisionElm = (
            <Row
              inheritStyle={style.discount}
              title={translate('calculator.commission')}
              discount={translate('currency_amount', {
                amount: offer.newInitialCommission + discountItem.amount,
              })}
              value={translate('currency_amount', {
                amount: offer.newInitialCommission,
              })}
            />
          );
        }
      });
    }
  };

我可以在调试器上看到该功能满足条件,但由于某种原因未发生重新分配?

有人可以帮我吗?谢谢负载

javascript react-native react-hooks
1个回答
0
投票

useEffect将在render方法完成在屏幕上的绘制之后调用。这意味着,如果您希望触发新的渲染,请在useEffect运行之后,触发STATE更改。我建议您要么将两个let变量定义为STATE,要么在状态中存储其他类型的数据,这些数据将在这两个var中使用。

© www.soinside.com 2019 - 2024. All rights reserved.