manuelBetancurt
manuelBetancurt

Reputation: 16168

React-native, is this way to set a variable in react correct?

I have the following code, I need to use a variable that is coming after a hook is set, how to use that in my render?

const LoanDetailCard = () => {  

  const loan = first(loanData?.loans); //var set from hook
  let interestRateOutter;
  let transactionalBalanceOutter;
  if (loan) {
    //set the outer variables with loanData???
    const {interestRate, transactionalBalance} = loan
    interestRateOutter = interestRate
    transactionalBalanceOutter = transactionalBalance
  }

  return (
    <View style={styles.container}> ...
   //now use interesRateOutter ??

OR

is the best approach just to check on the render elements?

<Text style={styles.textWhite} accessibilityLabel="Interest rate">
            {loan ? loan.interestRate : ""}
          </Text>

Upvotes: 0

Views: 32

Answers (1)

windowsill
windowsill

Reputation: 3649

You don't need any temporary variables in your render loop. Instead I would write it simply like:

const LoanDetailCard = (loanData) => {
  const { interestRate, transactionalBalance } = first(loanData);

  return <View style={styles.container}>
    <Text style={styles.textWhite} accessibilityLabel="Interest rate">
      {interestRate ?? ""}
    </Text>
  </View>;
};

Upvotes: 1

Related Questions