Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line 5: 'props' is not defined no-undef

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;
like image 753
razvanusc Avatar asked Dec 06 '25 23:12

razvanusc


1 Answers

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.

like image 152
Arup Rakshit Avatar answered Dec 08 '25 13:12

Arup Rakshit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!