I'm doing my first typescript project. This function is from a javascript vanilla project I did before, but when I use react-typescript I'm getting this error on both innerHTML from the function:
TypeError: Cannot set property 'innerHTML' of null
function callGames() {
let bets = document.getElementById('bets-container-lotos');
bets!.innerHTML = '';
const games = gamesJson.length;
for (let i = 0; i < games; i++) {
bets!.innerHTML +=
'<Loto id="bets-color-' +
i +
'" value="games' +
i +
'" OnClick={(' +
i +
') => whichGameIs}>' +
gamesJson[i].type +
'</Loto>';
let changeColor = document.getElementById('bets-color-' + i);
if (changeColor) {
changeColor.style.color = gamesJson[i].color;
changeColor.style.borderColor = gamesJson[i].color;
} else {
console.log('Cant find the id bets-color-' + i);
}
}
}
const newBet: React.FC = () => {
return (
<LotoContainer id='bets-container-lotos'>
</LotoContainer>)
}
document.getElementById('bets-container-lotos')is there by the moment when you call it?bets!.innerHTML = '';<- what's the point to cheat and use!.here?getElementById