In Part One, we learned how we can create a simple React element and add it in our page. You may check Part 1 here.
This was the state of our index.html page after the first part.
  1. <html>
  2. <head>
  3. <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  4. <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  5. </head>
  6. <body>
  7. <div id="app">
  8. </div>
  9. <script>
  10. // if(React){
  11. // alert('React is available')
  12. // }
  13. var el = React.createElement('h3',{id:'myheader',class:'test'},"Bilal Shahzad");
  14. ReactDOM.render(el,document.getElementById('app'));
  15. </script>
  16. </body>
  17. </html>

Function Component

Now let’s move element creation logic into a function and see another version of React.createElement() function.
  1. <script>
  2. function MyName(props){
  3. return React.createElement('h3',{id:'myheader',class:'test'},props.name);
  4. }
  5. var el = React.createElement(MyName, {name:"Bilal Shahzad"});
  6. ReactDOM.render(el,document.getElementById('app'));
  7. </script>
Explanation
We’ve moved our h3 element creation logic into a function. This function can be called “Function Component” in React. It is receiving an object which contains properties. Normally this is called “props” in React. (The name of variable can be anything). The function is creating React element and returning it. This function can be used multiple times now (reusability). Then we’ve passed function name to React.createElement() function as first parameter & passed properties (data/props) as second parameter. And then we’ve rendered this in our page DOM. Run page and check if it is still working.
Let’s create another component (function) which will create a profile link using anchor tag. We’ll pass properties (or dynamic content) as props to the function so it can be re-used with different content.
  1. <script>
  2. function ProfileLink(props){
  3. var url = props.url;
  4. var txt = props.urlText;
  5. return React.createElement('a',{href:props.url},txt);
  6. }
  7. var dataToPass = {url:"https://www.youtube.com/c/LearnInUrdu139",urlText: "Learn in Urdu Tutorials"};
  8. var el = React.createElement(ProfileLink, dataToPass);
  9. ReactDOM.render(el,document.getElementById('app'));
  10. </script>
Explanation
We’ve intentionally removed MyName component for the time being. We’ve created a new function component which is responsible to render a hyperlink. “props” contains URL & Anchor Text as we’ve passed it while using component.
You may have noticed that ReactDOM.render() is taking one element as first parameter. What if we want to render multiple elements instead (i.e. Name & profile link)? Let's say we want to generate the following HTML in our page through code (“app” div is empty by default).
  1. <div class="mycontainer">
  2. <h3>Bilal Shahzad</h3>
  3. <a href="https://www.youtube.com/c/LearnInUrdu139">Learn in Urdu Tutorials</a>
  4. </div>
This is what we’ll do,
  1. We’ll create a div element
  2. We’ll create a Name element as child of div
  3. We’ll create a Profile element as child of div
Here is the code snippet to do this,
  1. <body>
  2. <div id="app">
  3. </div>
  4. <script>
  5. function MyName(props){
  6. return React.createElement('h3',null,props.name);
  7. }
  8. function ProfileLink(props){
  9. var url = props.url;
  10. var txt = props.urlText;
  11. return React.createElement('a',{href:props.url},txt);
  12. }
  13. //Create name & profile elements
  14. var nameComp = React.createElement(MyName, {name:"Bilal Shahzad"});
  15. var dataToPass = {url:"https://www.youtube.com/c/LearnInUrdu139",urlText: "Learn in Urdu Tutorials"};
  16. var profileComp = React.createElement(ProfileLink, dataToPass);
  17. //create div element to hold name & profile elements
  18. var el = React.createElement(
  19. 'div'
  20. ,{class:'mycontainer'}
  21. ,nameComp
  22. ,profileComp);
  23. ReactDOM.render(el,document.getElementById('app'));
  24. </script>
  25. </body>
Explanation
We’ve created name & link elements using relevant components by providing required properties to them. Then we’ve created a parent (div) element using the same React.createElement() function. In this case we’ve provided multiple react elements as children, and yes, we may provide more. Then we’ve added it in DOM. If we run the page, the following should be output & html generated by react.
react
Now shouldn't we have a profile component which shows one person's profile (name & link)? Let’s convert the newly-created div into a separate component so we can reuse it. Don’t get confused, please check comments in the snippet & the explanation below for better understanding.
  1. <body>
  2. <div id="app">
  3. </div>
  4. <script>
  5. function MyName(props){
  6. return React.createElement('h3',null,props.name);
  7. }
  8. function ProfileLink(props){
  9. var url = props.url;
  10. var txt = props.urlText;
  11. return React.createElement('a',{href:props.url},txt);
  12. }
  13. function Profile(props){
  14. //Create name & profile elements
  15. var nameComp = React.createElement(MyName, props);
  16. var profileComp = React.createElement(ProfileLink, props);
  17. //create div element to hold name & profile elements
  18. var el = React.createElement(
  19. 'div'
  20. ,{class:'mycontainer'}
  21. ,nameComp
  22. ,profileComp);
  23. return el;
  24. }
  25. //create object to pass as props
  26. var personData = {
  27. name : "Bilal Shahzad",
  28. url:"https://www.youtube.com/c/LearnInUrdu139",
  29. urlText: "Learn in Urdu Tutorials"
  30. };
  31. var el = React.createElement(Profile,personData);
  32. ReactDOM.render(el,document.getElementById('app'));
  33. </script>
  34. </body>
Explanation
We’ve moved code to create a “div” element in a separate function (component). We are going to pass dynamic content from outside as props. We may pass specific props to name & link components but we’ve provided the same props object to them for our ease. Then we’ve created a single object which contains name, url & urlText. Then we created profile element. Run page & it should be working fine.
Now what if we want to show another profile on the page? Simple, we just need to:
  • Create another data object of that profile
  • Create another profile element
  • Wrap both profile elements in some container and add that container in DOM.
Here is the code snippet to show how to do this (Note: No change in components)
  1. //create object to pass as props
  2. var personData = {
  3. name : "Bilal Shahzad",
  4. url:"https://www.youtube.com/c/LearnInUrdu139",
  5. urlText: "Learn in Urdu Tutorials"
  6. };
  7. //create profile element
  8. var profile1 = React.createElement(Profile,personData);
  9. //create object to pass as props
  10. var personData2 = {
  11. name : "Faisal Shahzad",
  12. url:"https://www.youtube.com/c/LearnInUrdu139",
  13. urlText: "Learn in Urdu Tutorials 2"
  14. };
  15. //create profile element
  16. var profile2 = React.createElement(Profile,personData2);
  17. var el = React.createElement(
  18. 'div'
  19. ,{class:'maincontainer'}
  20. ,profile1
  21. ,profile2);
  22. ReactDOM.render(el,document.getElementById('app'));
Explanation
We’ve just used our profile component multiple times. We created 2 profile elements and then added them in a div element as we need a single element to add in DOM. We then added the div element in DOM. If we run the page, the following should be output & html generated by react.
react
But wait, what? Is this React? Is this creating ease for us or making things difficult? A normal page has many elements, are we going to create components like this? Where are our own tags e.g. <Person>? Yes, you may use this approach (i.e. using React.createElement()) functions or optionally use JSX to ease things.

What is JSX?

  • JSX stands for JavaScript XML
  • It is an HTML-like syntax
  • JavaScript & HTML can be mixed
  • It just providesthe sugar coating of syntax we’ve seen above
Let’s check an example of creating React elements using JSX
  1. function MyName(props){
  2. //return React.createElement('h3',{id:'myheader',class:'test'},props.name);
  3. return <h3 id='myheader' class='test'>{props.name}</h3>
  4. }
Explanation
We can create our elements like we do with HTML. But here we can mix JavaScript by using curly braces (e.g. {props.name}
But before we start using it, we need to do a couple of things.
Import the following script in head, now there should be three references.
  1. <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  2. <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  3. <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
Update script tag with the following:
  1. <script type='text/babel'>
Here is the full snippet which is creating a Name component by using JSX syntax.
  1. <html>
  2. <head>
  3. <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  4. <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  5. <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
  6. </head>
  7. <body>
  8. <div id="app">
  9. </div>
  10. <script type='text/babel'>
  11. //Component is still return react element
  12. function MyName(props){
  13. return <h3 id='myheader' class='test'>{props.name}</h3>
  14. }
  15. //Created a top level component
  16. function MyApp(){
  17. return (
  18. //Using our custom tag (component), passing data as properties
  19. <MyName name="Bilal Shahzad"/>
  20. );
  21. }
  22. ReactDOM.render(<MyApp />,document.getElementById('app'));
  23. </script>
  24. </body>
  25. </html>
Explanation
We’ve updated MyName() component and now we are returning React element using JSX syntax. We are using HTML & mixing JavaScript by using curly braces. We’ve also created a top level component which will hold our other components. In MyApp() component, we’ve used MyName component as we use HTML tags. Now we are passing properties to component (e.g. name=”Bilal Shahzad”) as we set in HTML. Then we’ve used <MyApp /> syntax in ReactDOM.render() method.
Let’s try to convert our Persons example using JSX. It should be something like this.
  1. <html>
  2. <head>
  3. <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  4. <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  5. <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
  6. </head>
  7. <body>
  8. <div id="app">
  9. </div>
  10. <script type='text/babel'>
  11. function MyName(props){
  12. return <h3>{props.name}</h3>
  13. }
  14. function ProfileLink(props){
  15. return <a href={props.url}>{props.urlText}</a>;
  16. }
  17. function Profile(props){
  18. return (
  19. <div class="mycontainer">
  20. <MyName name={props.name} />
  21. <ProfileLink url={props.url} urlText={props.urlText} />
  22. </div>
  23. );
  24. }
  25. //Created a top level component
  26. function MyApp(){
  27. return (
  28. <div class="maincontainer">
  29. <Profile name="Bilal Shahzad" url="https://www.youtube.com/c/LearnInUrdu139" urlText="Learn in Urdu Tutorials" />
  30. <Profile name="Faisal Shahzad" url="https://www.youtube.com/c/LearnInUrdu139" urlText="Learn in Urdu Tutorials 2" />
  31. </div>
  32. );
  33. }
  34. ReactDOM.render(<MyApp />,document.getElementById('app'));
  35. </script>
  36. </body>
  37. </html>
Explanation
We’ve updated MyName, ProfileLink & Profile components with JSX approach. Then we’ve created two profiles in MyApp component. We can see how simplified it has become with JSX. It is more readable now.
So we’ve learnt that we can create a React application using 1) React plain code OR 2) Using JSX

What is Babel?

Babel is a JavaScript compiler (or transpiler) that includes the ability to compile JSX into regular JavaScript. When it is used with React, it converts JSX into React code.
To understand it better
  1. Go to https://babeljs.io/repl. It will show us what it generates against JSX
  2. Copy following JSX code and paste in left panel, it will show generated react code in right panel
  1. var name = "Bilal Shahzad";
  2. <h3 id="test">{name}</h3>

Practice Tasks

  1. Create a new array of objects in MyApp and render multiple profiles against it. Hint: You need to use map function to iterate array.
  2. Check what react code is generated against JSX we’ve after Task 1.

Conclusion

In this part, we’ve extended our learning of React and learned about “Function Components”. A function component takes properties as input and returns a React element. We learned how JSX eases our work by allowing us to use HTML like syntax with a mixture of JavaScript statements.
For more reading on JSX,
https://reactjs.org/docs/jsx-in-depth.html