I'm trying to create a randomiser for the data I get from the API; however I get this "Line 5: 'props' is not defined no-undef" error and don't see what's wrong with it.
import React from "react";
class Random extends React.Component {
constructor() {
super(props);
this.state = {
breweries: []
};
}
componentDidMount() {
fetch("https://api.openbrewerydb.org/breweries")
.then(response => response.json())
.then((data) => {
this.setState({
breweries: data,
})
})
}
render() {
const brewery = this.state.breweries[Math.floor(Math.random()*this.state.breweries.length)];
return(
<div>{brewery.id}</div>
)
}
}
export default Random;
You are missing constructor(props) {. props first needs to be received by the constructor function as an argument, then only you can use it. You are missing that part. Otherwise, you will get Uncaught ReferenceError: props is not defined error at runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With