React JS Nested Components
React Component Creation
var Component React.createClass({ render: function(){ return ( <Something></Something> )}});
This is just a basic component that returns a set of <Something> HTML tags. I know, you’re probably still getting used to returning something that looks like an HTML tag without quotes around it, but by this time it should start looking more familiar 🙂 As you may have imagined, this is the type of code structure you will be working a lot within React JS. Most other things have come from it. If you’ve ever worked with jQuery, this component structure can be comparable to jQuery.
Object-selector Syntax
$(".class").on("click", function() {/ *do something*/});
Here we have a callback function that returns the event has finished executing. In react, we’re returning an HTML element that will usually be placed inside our application’s structure on the front end. In the case of React components, it’s just a JavaScript object that contains render function and has a return statement that returns transpired JSX (JavaScript XML) code. That’s what converts <Something> tag into EcmaScript 5 that browsers can understand.
Nesting React JS Components Examples
Parent with a Single Child – React JS Nested Components
Parent with a Single Child Example
var Child = React.createClass({ render: function() { return <BUTTON onClick {this.props.onClick }> {this.props.text } </BUTTON> } }) ;
In this case, the child is a basic component that returns a button element. The button provides an onClick event that will be executed by the method this.props.onClick.
this.props.onClick Example – React JS Nested Components
var Parent = React.createClass({ render: function() { return( <Child onClick = {this.handleClick} text ={this.state.child Text}/> ); }, handle Child Click: function(event) { // In this method, you can access props // that were passed to the Child, using this.state property var child_text = this.state.childText; // To access target element of the click: var target = event.target; } }) ;
Notice that you can access a child’s text from the handleChildClick method within Parent container. The Parent and Child containers are tied by the props passed into the text attribute on the Child container. Whatever you pass into the Child (which is located within Parent) will be automatically accessible via the parent container. So props are really what establishes the relationship between the two.
Parents with Multiple Children – React JS Nested Components
Parents with Multiple Children Example
var Parent = React.createClass({ / Set defaults for multiple children array / getChildren State: function() { return children Data:[ { check Text: "Click me 1!", child Number: 1 }, (child Text;"Click me 2t", child Number: 2} ]}; }, render: function()[ return( var children - this.state.childernData.map(function( data, index){ return <Child onClick=(this.handleClick bind(null, child Data))} text = {child Data.child Text)/;/>; }.bind(this)); return <DIV>{children]</DIV>; ) ; } handle Child Click: function(event) { // In this method, you can access props // that were passed to the Child, using this.state propert var child_text=this.state.childText; // To access target element on the click var target = event.target; } }) ;
Now, I purposely created these generic examples to show you the association between child and parent, because it’s not as trivial as simply nesting HTML elements in one another. In react JS, this link is established programmatically, as shown in the example above.