Skip to content
Advertisement

How to fix “TypeError: props.scoreboard.map is not a function”, only after refresh

I’m having an odd issue when I’m trying to loop over my response from my backend. I’ve seen similar questions and they seem to point to the response not being an array, but logging my response does indeed result in an array.

I’m passing the scoreboard prop to the scoreBoardItem which will map over the props and return what I want.

JavaScript

This is the ScoreBoardItem component, strangely if I comment out the map function, rerun then uncomment it; it will work. It will then stop working again after refreshing.

JavaScript

Appreciate any thoughts on this matter.

Advertisement

Answer

This happens because when defining const [scoreboard, setScoreboard] = useState(''); you don’t define it with the initial value of [”]

Replace it with const [scoreboard, setScoreboard] = useState(['']);

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement