New to react/next, sorry if this is obvious, i tried searching and tried the solutions to similar problems but i think im missing something or not understanding correctly. I am aware that loadStats() gets infinitely called because it's called inside of the render function, im just not sure how to solve it...
I tried onClick={loadStats(stats.player).bind(stats.player)}
it just errors bind() is not a function... heres the component.
import React, { useEffect, useState } from "react";
import Teams from "./Teams";
const SquadMembers = ({ squadz }) => {
const [teams, setTeams] = useState([]);
const loadStats = async (player) => {
const req = await fetch(`/api/player/${player}`);
const json = await req.json();
setTeams(json);
};
if (!squadz.teamStats) {
return null;
}
let member = squadz.playerStats;
return member.map((player, index) => (
<React.Fragment key={index}>
<br />{" "}
<div>
<br />
{player.map((stats) => (
<React.Fragment key={stats.player}>
<div class="flex-child">
<button value={stats.player} onClick={loadStats(stats.player)}>
{stats.player}
</button>
<p>{stats.role}</p>
<p>{stats.win_rate}</p>
<p>{stats.matches}</p>
<p>{stats.total_battles} Total Battles</p>
<p>{stats.score} / Points Earned</p>
</div>
</React.Fragment>
))}
</div>
<br />
<Teams teams={teams} />
</React.Fragment>
));
};
export default SquadMembers;