# Creating Complex Components

**URL:** https://forum.kirupa.com/t/creating-complex-components/636222
**Category:** programming
**Created:** [March 20, 2017, 5:23am UTC](https://forum.kirupa.com/t/creating-complex-components/636222 "2017-03-20T05:23:57Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [March 20, 2017, 5:23am UTC](https://forum.kirupa.com/t/creating-complex-components/636222/1 "2017-03-20T05:23:58Z")

</div>

In a [previous article](http://www.kirupa.comcomponents.htm), we learned about components and all the awesome things that they do. We learned that components are the primary ways through which React allows our visual elements to behave like little reusable bricks that contain all of the HTML, JavaScript and styling needed to run themselves. Beyond reusability, there is another major advantage components bring to the table. They allow for _ **composability** _. You can combine components to create more complex components.

* * *
This is a companion discussion topic for the original entry at [http://www.kirupa.com/react/creating\_complex\_components.htm](http://www.kirupa.com/react/creating_complex_components.htm)

---

<div class="post-metadata">

### Author: ![Finnegan](https://avatars.discourse-cdn.com/v4/letter/f/8e8cbc/32.png) [@Finnegan](https://forum.kirupa.com/u/Finnegan)
#### Post date: [April 8, 2017, 2:12am UTC](https://forum.kirupa.com/t/creating-complex-components/636222/2 "2017-04-08T02:12:57Z")

</div>

Outstanding tutorial, outstanding writing.

You have by far the best tutorials for the React beginner that I’ve found. Keep up the great work!

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [April 8, 2017, 2:53am UTC](https://forum.kirupa.com/t/creating-complex-components/636222/3 "2017-04-08T02:53:28Z")

</div>

Thanks, Finnegan! Glad you find them useful. If there are additional topics that you would like me to cover, please let me know 🙂

---

<div class="post-metadata">

### Author: ![nip90](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@nip90](https://forum.kirupa.com/u/nip90)
#### Post date: [June 13, 2017, 5:12pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/4 "2017-06-13T17:12:58Z")

</div>

Very good tutorial.  
I’m stumbling on behavior I can’t explain.  
If I’m using the following code, I see the square

  

```
	var Square = React.createClass({
		render: function() {
			
			return(
				<div>
			
				</div>
			);
		}
	});
	var Card = React.createClass({
	  	render: function() {
	      	var cardStyle = {
		        height: 200,
		        width: 150,
		        padding: 0,
		        backgroundColor: "#FFF",
		        WebkitFilter: "drop-shadow(0px 0px 5px #666)",
		        filter: "drop-shadow(0px 0px 5px #666)"
	        };
		    return (
		 		<div style={cardStyle}>
					
        		</div>
		    );
	    }
	});

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

```

However, If I use this code (omitting the Div in the square class), I see nothing

  

```
	var Square = React.createClass({
		render: function() {
			
			return(
			
			);
		}
	});
	var Card = React.createClass({
	  	render: function() {
	      	var cardStyle = {
		        height: 200,
		        width: 150,
		        padding: 0,
		        backgroundColor: "#FFF",
		        WebkitFilter: "drop-shadow(0px 0px 5px #666)",
		        filter: "drop-shadow(0px 0px 5px #666)"
	        };
		    return (
		 		<div style={cardStyle}>
					
        		</div>
		    );
	    }
	});

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

```

Why the Div matter if I do not reference the Square class at all?

Regards,  
N.

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [June 13, 2017, 6:07pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/5 "2017-06-13T18:07:48Z")

</div>

The reason has to do with JSX being turned into regular JavaScript without knowing what code will be hit or not. When the Babel transpiler runs, it turns your first version of your code into this:

```
var Square = React.createClass({
	displayName: "Square",

	render: function render() {

		return React.createElement("div", null);
	}
});

```

When you don’t specify anything to return, you get an error because the `render` method must call `React.createElement`. Not having anything there causes an error like **Unexpected Token** or something equivalent.

🙂

---

<div class="post-metadata">

### Author: ![bap](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bap/32/8543_2.png) [@bap](https://forum.kirupa.com/u/bap)
#### Post date: [July 6, 2017, 9:36am UTC](https://forum.kirupa.com/t/creating-complex-components/636222/6 "2017-07-06T09:36:52Z")

</div>

And in fact you can work around this by slightly modifying the example (and supporting prose) like this:

```
var Square = React.createClass({
	render: function() {
		
		return;
	}
});

```

As you can see I simply omited the parenthetical marks after the return which was empty context. Tested on both Chrome and electron (Chromium.) After doing this it transpiles and executes a-ok allowing you to check your work as you go rather than ‘all at the end.’

---

<div class="post-metadata">

### Author: ![nbryson](https://avatars.discourse-cdn.com/v4/letter/n/6f9a4e/32.png) [@nbryson](https://forum.kirupa.com/u/nbryson)
#### Post date: [August 24, 2017, 8:39pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/7 "2017-08-24T20:39:20Z")

</div>

Hello, here is the code in between my script tags that I have and nothing is displaying. I have read and reread the example and explanation in the tutorial, but still can’t get it to work. Is there anything you can see here that would help point me in the right direction? I want to complete the tutorial but can’t get passed this stage. Help!

```auto
var Square = React.createClass({
render: function(){

var = squareStyle = {
	heght: 150,
	backgroundColor: this.props.color

}

};

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

}

});

var Label = React.createClass({

render: function() {

var labelStyle = {
      fontFamily: "sans-serif",
      fontWeight: "bold",
      padding: 13,
      margin: 0
	  
return(
<p style={labelStyle}>{this.props.color}</p>
);

}

});

var Card = React.createClass({

render: function() {
var cardStyle = {
        height: 200,
        width: 150,
        padding: 0,
        backgroundColor: "#FFF",
        WebkitFilter: "drop-shadow(0px 0px 5px #666)",
        filter: "drop-shadow(0px 0px 5px #666)"
      };
return(
<div style={cardStyle}>
	
	<Square color={this.props.color}/>
    <Label color={this.props.color}/>
	</div>
);

}

});

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

```

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [August 25, 2017, 3:42am UTC](https://forum.kirupa.com/t/creating-complex-components/636222/8 "2017-08-25T03:42:31Z")

</div>

Is your console showing any errors? I don’t access to a laptop right now, but I can check in more detail tomorrow 🙂

---

<div class="post-metadata">

### Author: ![nbryson](https://avatars.discourse-cdn.com/v4/letter/n/6f9a4e/32.png) [@nbryson](https://forum.kirupa.com/u/nbryson)
#### Post date: [August 31, 2017, 6:21pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/9 "2017-08-31T18:21:53Z")

</div>

I’ll look at this code again soon and get back to you. I appreciate your quick response. Thx!

---

<div class="post-metadata">

### Author: ![nbryson](https://avatars.discourse-cdn.com/v4/letter/n/6f9a4e/32.png) [@nbryson](https://forum.kirupa.com/u/nbryson)
#### Post date: [September 21, 2017, 7:28pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/10 "2017-09-21T19:28:43Z")

</div>

Hi, there was an error, and I watched the video and worked through it. Thx! But I have into another snag in the “Dealing with State” section. There is another unexpected error in the console. I see the carrot, but don’t understand what exactly is the issue. I have the same exact code in the example. Can you point me in the right direction?

```
/////
class LightningCounter extends React.Component {
                constructor(props, context) {
                    super(props, context);
                     
                    this.state = {
                        strikes: 0
                    };
 
                    this.timerTick = this.timerTick.bind(this);
                }
 
                timerTick = () => {
                    this.setState({
                        strikes: this.state.strikes + 100
                    });
                }
 
                componentDidMount() 
                {
                    setInterval(this.timerTick, 1000);
                }
                 
                render() {
                    var counterStyle = {
                        color: "#66FFFF",
                        fontSize: 50
                    };
 
                    var count = this.state.strikes.toLocaleString();
 
                    return (
                        <h1 style={counterStyle}>{count}</h1>
                    );
                }
            }
 
            class LightningCounterDisplay extends React.Component {
                render() {
                    var commonStyle = {
                        margin: 0,
                        padding: 0
                    };
 
                    var divStyle = {
                        width: 250,
                        textAlign: "center",
                        backgroundColor: "#020202",
                        padding: 40,
                        fontFamily: "sans-serif",
                        color: "#999999",
                        borderRadius: 10
                    };
 
                    var textStyles = {
                        emphasis: {
                            fontSize: 38,
                            ...commonStyle
                        },
                        smallEmphasis: {
                            ...commonStyle
                        },
                        small: {
                            fontSize: 17,
                            opacity: 0.5,
                            ...commonStyle
                        }
                    }
 
                    return(
                        <div style={divStyle}>
                            <LightningCounter/>
                            <h2 style={textStyles.smallEmphasis}>LIGHTNING STRIKES</h2>
                            <h2 style={textStyles.emphasis}>WORLDWIDE</h2>
                            <p style={textStyles.small}>(since you loaded this example)</p>
                        </div>
                    );
                }
            }
 
            ReactDOM.render(
                <LightningCounterDisplay/>,
                document.querySelector("#container")
            )

////
```

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [September 21, 2017, 9:07pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/11 "2017-09-21T21:07:52Z")

</div>

Can you copy and paste the error from the console? 🙂

---

<div class="post-metadata">

### Author: ![nbryson](https://avatars.discourse-cdn.com/v4/letter/n/6f9a4e/32.png) [@nbryson](https://forum.kirupa.com/u/nbryson)
#### Post date: [September 22, 2017, 2:58pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/12 "2017-09-22T14:58:02Z")

</div>

Here you go! Thx again!

```
Uncaught SyntaxError: embedded: Unexpected token (13:26)
  11 | }
  12 |  
> 13 | timerTick = () => {
     | ^
  14 | this.setState({
  15 | strikes: this.state.strikes + 100
  16 | });
```

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [September 22, 2017, 10:34pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/13 "2017-09-22T22:34:11Z")

</div>

I made a mistake when specifying the current syntax for `timerTick`. Try this instead:

```
timerTick() {
    this.setState({
        strikes: this.state.strikes + 100
    });
}
```

---

<div class="post-metadata">

### Author: ![oalbacha](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/oalbacha/32/8883_2.png) [@oalbacha](https://forum.kirupa.com/u/oalbacha)
#### Post date: [December 21, 2017, 1:57pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/14 "2017-12-21T13:57:55Z")

</div>

Can’t seem to see the card for some reason. Can you help see if there is any error in my code or dependencies or whatever please:

```
<!DOCTYPE html >
<html lang = "en" >
    <head >
        <meta charset = "UTF-8" >
        <meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
        <meta http - equiv = "X-UA-Compatible" content = "ie=edge" >
        <title> More Components </title> 

        <script src = "https://unpkg.com/react@16/umd/react.development.js" > </script> 
        <script src = "https://unpkg.com/react-dom@16/umd/react-dom.development.js" > </script> 
        <script src= "https://unpkg.com/babel-standalone@6/babel.min.js" > </script>

        <style> 
            #container {
                padding: 50 px;
                background - color: #FFF;
            } 
        </style> 
    </head>

    <body >
        <div id = "container" >
        
        </div>
        
        <script type = "text/babel" >
            

            var Square = React.createClass({
              render: function() {
                return (
                  <div/>
                );
              }
            });

            var Label = React.createClass({
              render: function() {
                return (
                  <div/>
                );
              }
            });

            var Card = React.createClass({
              render: function() {
                var cardStyle = {
                  height: '200px',
                  width: '150px',
                  padding: 0,
                  backgroundColor: '#FFF',
                  WebkitFilter: 'drop-shadow(0px 0px 5px #666)',
                  filter: 'drop-shadow(0px 0px 5px #666)'
                };
                return (
                  <div style= {cardStyle}>

                  </div>
                );
              }
            });

            var destination = document.querySelector('#container');

            ReactDom.render(
                <div>
                  <Card/>
                </div>,
                destination
            );
        </script> 
    </body>
</html>
```

---

<div class="post-metadata">

### Author: ![oalbacha](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/oalbacha/32/8883_2.png) [@oalbacha](https://forum.kirupa.com/u/oalbacha)
#### Post date: [December 21, 2017, 2:14pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/15 "2017-12-21T14:14:48Z")

</div>

My inspect console error is the following:

> Uncaught TypeError: React.createClass is not a function

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [December 26, 2017, 3:19am UTC](https://forum.kirupa.com/t/creating-complex-components/636222/16 "2017-12-26T03:19:13Z")

</div>

You have to use the `class` syntax as opposed to `React.createClass`, starting with React 16. The code in the tutorial should work for you, for it is using the latest syntax! The video isn’t updated yet, but that is something I will try to re-record later this week 🙂

---

<div class="post-metadata">

### Author: ![oalbacha](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/oalbacha/32/8883_2.png) [@oalbacha](https://forum.kirupa.com/u/oalbacha)
#### Post date: [December 26, 2017, 3:28pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/17 "2017-12-26T15:28:35Z")

</div>

Would you post a blog or notify us followers when you do update the videos please. Many thanks,

---

<div class="post-metadata">

### Author: ![Jamadalama](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jamadalama/32/10536_2.png) [@Jamadalama](https://forum.kirupa.com/u/Jamadalama)
#### Post date: [December 10, 2019, 8:04pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/18 "2019-12-10T20:04:43Z")

</div>

Good stuff man this book has been helpful and greatly appreciated.

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [December 11, 2019, 1:43pm UTC](https://forum.kirupa.com/t/creating-complex-components/636222/19 "2019-12-11T13:43:50Z")

</div>

Thanks! Glad you enjoyed it 🙂
