I'm getting data from an API but I cant seem to get an onClick listener to work with the returned results
async componentDidMount() {
const response = await fetch('/api/mediabuys');
await response.json().then(data => {
let markets = data.mediabuys.map(function(item) {
return(
<tr key={item.id}>
<td onClick={this.openWindow.bind(this)}>{item.name}</td>
</tr>
)
})
this.setState({markets: markets});
})
}
openWindow() {
console.log('a');
}
and the error im getting is:
Uncaught (in promise) TypeError: Cannot read property 'openWindow' of undefined
if I add in this.openWindow
to the beginning of ComponentDidMount, its able to run the function and I also have this.openWindow = this.openWindow.bind(this)
in the constructor. How can I get the onClick to see the function?