how to setstate in if react native

I’m trying to export exportTest to another js file and do re-render. I’m trying it like this

import React, { useState } from 'react';
import { StyleSheet, TouchableOpacity, View, Text } from 'react-native';

var n = 0;
export var exportTest;

export default function App () {

  const [test,setTest] = useState();
  if(n == 0)
  {
    setTest('n is zero')
  }
  else
  {
    setTest('n is not zero')
  }
  exportTest=test

  return(
     ...
     ...
     ...
  );
}

I’m getting error too meny re-renders, because I’m doing setTest in if
How can I do this? n is changing in some function, I’m getting it as 1 or 0.
And this is how I’m importing exportTest in another file import {exportTest} from './App.js';

Source: Ask Javascript Questions

LEAVE A COMMENT