ReferenceError: cardStyle is not defined


#1

With this code I get this error. I’ve looked and looked but can’t seem to find the problem. Any suggestions?

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>More Components</title>
    <script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
    <script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/[email protected]/babel.min.js"></script>
    <style>
      #container {
        padding: 50px;
        background-color: #FFF;
      }
    </style>
  </head>
    <body>
    <div id="container"></div>
      <script type="text/babel">

      class Card extends React.Component {
      render() {

      var cardStyle = {
        height: 200,
        width: 150,
        padding: 0,
        backgroundColor: "#FFF",
        boxShadow: "0px 0px 5px #666"
      };

        return (
          <div style={cardStyle}>
          </div>
        );
      }
      }

      class Square extends React.Component {
      render() {

        var squareStyle = {
          height: 150,
          width: 150,
          backgroundColor: "#FF6663"
        };

        return(
            <div style={squareStyle}>
             </div>
          );
      }
      }

        class Label extends React.Component {
        render() {

          var labelStyle = {
            fontFamily: "sans-serif",
            fontWeight: "bold",
            padding: 13,
            margin: 0
          };

          return (
            <p style={labelStyle}>#FF6663</p>
          );
        }
        }



      ReactDOM.render(
        <div style={cardStyle}>
          <Square/>
            <Label/>
          </div>,
          document.querySelector("#container")
        );
      </script>
    </body>
</html>

#2

That was a tricky one! It took me a few passes to figure it out, for the error messages in the console weren’t very helpful haha. Your final ReactDOM.render call should just be to display the Card component:

ReactDOM.render(
  <div>
    <Card/>
  </div>,
  document.querySelector("#container")
);

Currently, it is referencing the cardStyle object, and that object doesn’t exist in a way for this component to find it. This is the missing reference error that we both saw! It wasn’t a misspelling or something with the cardStyle used inside the Card component itself :rofl:

The Card component contains both the reference to cardStyle as well as the child components, Square and Label:

class Card extends React.Component {
  render() {
    var cardStyle = {
      height: 200,
      width: 150,
      padding: 0,
      backgroundColor: "#FFF",
      boxShadow: "0px 0px 5px #666"
    };

    return (
      <div style={cardStyle}>
          <Square />
          <Label />
      </div>
    );
  }
}

Once you make these two minor updates, your code should totally work! :slight_smile:

Cheers,
Kirupa