What we’ll learn in this part:
More on JSX
Let’s learn more about JSX as this will help us in fixing most common issues while working with React/JSX.
JSX is:
- HTML like syntax in which you can write JavaScript expressions in curly braces.
- OR JavaScript code in which you can use HTML like tags.
With JSX, when we are writing HTML & want to write some JavaScript within it, we need to use curly braces {}. We can’t have multiple statements in this. So can we use a “for” loop or if statement in these curly braces? The answer is "No". If we need to have complex logic which requires multiple statements (or curly braces), we should create a separate function and then call that function in JSX curly braces or prepare final result separately and place that in curly braces.
User defined components must be capitalized.
When we start element with lower case, React refers to built-in tags (e.g. div, h1).
For example, in following code “MyName” is started with lowercase (“myName”) so React will think that it is HTML tag.
- <script type='text/babel'>
- function MyName(props){
- return <h3>{props.name}</h3>
- }
- //Created a top level component
- function MyApp(){
- return (
- <div class="maincontainer">
- {/*Correct*/ }
- <MyName name="Bilal Shahzad 1" />
- {/*will give error*/ }
- <myName name="Bilal Shahzad 2" />
- </div>
- );
- }
- ReactDOM.render(<MyApp />,document.getElementById('app'));
- </script>
If we check on the console, we’ll see the following error:

Assume these tags are objects and yes they are, i.e., React Elements.
If these tags are objects, can we store them in some variable? Can we add them in an array? Can we pass them to a function? Can we return them from a function? Yes, we can.
- function MyApp(){
- var o = <Profile name="Bilal Shahzad" url="https://www.youtube.com/c/LearnInUrdu139" urlText="Learn in Urdu Tutorials" />
- console.log(o);
- return o;
- }
If we check output on console, we can see that it is a React element which contains ‘props’ property in it.

Here is another example
- function MyApp(){
- var h = <h3>Profiles</h3>
- console.log(h);
- var o = <Profile name="Bilal Shahzad" url="https://www.youtube.com/c/LearnInUrdu139" urlText="Learn in Urdu Tutorials" />
- var o1 = <Profile name="Faisal Shahzad" url="https://www.youtube.com/c/LearnInUrdu139" urlText="Learn in Urdu Tutorials 2" />
- return [h,o,o1];
- }

Note
When we want to return multiple elements from our component without a parent, we can return array element as used above. Here is another example.
When we want to return multiple elements from our component without a parent, we can return array element as used above. Here is another example.
- function MyApp(){
- var h = <h3>Profiles</h3>
- //returning multiple elements without parent
- return (
- [
- h,
- <Profile name="Bilal Shahzad" url="https://www.youtube.com/c/LearnInUrdu139" urlText="Learn in Urdu Tutorials" />,
- <Profile name="Faisal Shahzad" url="https://www.youtube.com/c/LearnInUrdu139" urlText="Learn in Urdu Tutorials 2" />
- ]);
- }
Let’s update our MyApp component and render different profiles programmatically.
Let’s assume we have data as an array of objects. We want to generate Profiles based on this data. We need to iterate this array and generate Profile components. But we can’t use for loop in JSX curly braces so we’ve prepared our result separately and then just placed that in curly braces to render it.
- <script type='text/babel'>
- function MyName(props){
- return <h3>{props.name}</h3>
- }
- function ProfileLink(props){
- return <a href={props.url}>{props.urlText}</a>;
- }
- function Profile(props){
- return (
- <div class="mycontainer">
- <MyName name={props.name} />
- <ProfileLink url={props.url} urlText={props.urlText} />
- </div>
- );
- }
- //Created a top level component
- function MyApp(){
- //hard coded data for now
- var data = [
- {name:"Bilal Shahzad",url:"https://www.youtube.com/c/LearnInUrdu139",urlText:"Learn in Urdu Tutorials"},
- {name:"Faisal Shahzad",url:"https://www.youtube.com/c/LearnInUrdu139",urlText:"Learn in Urdu Tutorials 2"}
- ];
- //create an array to hold profile objects
- var result = [];
- for(var i=0;i<data.length;i++){
- var obj = data[i];
- result.push(<Profile name={obj.name} url={obj.url} urlText={obj.urlText} />)
- }
- return (
- <div class="maincontainer">
- {result}
- </div>
- );
- }
- ReactDOM.render(<MyApp />,document.getElementById('app'));
- </script>
Let’s update the above code & create another component, Profiles, to hold this logic. We are passing ‘data’ object in props.

Rushi MehtaPosted Jan 22, 2020, 11:50 AM
Nice Startup