Skip to main content

Command Palette

Search for a command to run...

Resolving undefined errors while using map function in jsx.

Updated
1 min readView as Markdown
If you are calling an api and stored data in useState. sometimes you may get an error called 'undefined' when you use map in your jsx.


ex: const [responseData, setResposeData] = useState();

axios.get('http://url').then(resp => {
    setResposeData(resp)
});

responseData.map((item)=>
//your code
)

then add ? it works like a try and catch block and avoids undefined errors.

responseData?.map((item)=>
//your htlml code
)

More from this blog

Chowhan's blog

29 posts

A Passionate software developer