Why does it run many times?
It is because whenever you set to a state like using setTokensList in your example, this will tell React to execute all the javascript code in your component again starting from top to bottom in the same order.
To avoid executing some of your code every time but only upon some variables changes, you should use useCalback
or useMemo
React hooks.
In your case you have a side effect code like loading data from a remote endpoint. In this case it is better to be done in the componentDidMount lifecycle hook. Because your component will execute your code in this hook only once after the first render and not multiple times where you don't want to in your situation.
If you are using react hooks, you can achieve that by placing your code in the useEffect hook as follows:
const [tokenslist, setTokensList] = useState([])
useEffect(()=> {
fetch("http://localhost:81/getTokensList")
.then(response => response.json())
.then(data => {
setTokensList(data);
console.log("TOKENS", data);
query(data);
});
}, []);