In the previous page we looked at JSX syntax where we displayed a string Hello World inside the div element. What's the big deal! What else can it do for us?

Since JSX is technically Javascript it is pretty powerful in many sense. It can do everything that Javascript can do.

If you want to execute any Javascript code within JSX then you surround your Javascript code with curly braces { //javascript code } and put it inside anywhere in JSX. It will evaluate your code everytime it renders the component to find out what it should render on the browser.

For example lets imagine we want to display a company profile information and a Company ticker. And imagine the Company ticker is stored on some variable somewhere and the company profile information is stored in another variable as an object. In that case we might write:

  1. function CompanyProfile(props) {
  2. //ticker and companyProfileInfo stored in a variable
  3. const ticker = 'AAPL';
  4. const companyProfileInfo = {
  5. 'Company Name': 'Apple Inc.',
  6. 'Exchange': 'Nasdaq',
  7. 'Sector': 'Technology',
  8. 'Industry': 'Computer Hardware',
  9. 'CEO': 'Timothy D. Cook'
  10. };
  11. return (
  12. <div>
  13. <div>Profile of: {ticker}</div>
  14. <div>
  15. {
  16. {/*This is javascript code inside the curly braces*/}
  17. Object.keys(companyProfileInfo)
  18. .map((key, index) => {
  19. return <div>{key}: {companyProfileInfo[key]}</div>
  20. })
  21. }
  22. </div>
  23. </div>
  24. )
  25. }

The HTML output of the above Component when it's rendered will be:

  1. <div>
  2. <div>Profile of: AAPL</div>
  3. <div>
  4. <div>Company Name: Apple Inc.</div>
  5. <div>Exchange: Nasdaq</div>
  6. <div>Sector: Technology</div>
  7. <div>Industry: Computer Hardware</div>
  8. <div>CEO: Timothy D. Cook</div>
  9. </div>
  10. </div>

Well that's a handful, so let's review what's happening here. We have a ticker variable assigned to a value AAPL and an object companyProfileInfo that has company profile. Inside the JSX (inside the return statement) we have a div enclosing everything. In JSX, a component must return one and only one enclosing tag. That tag can have as many children as it wants.

  1. // ❌ This is illegal in React since the return has more than one tag.
  2. return (
  3. <div></div>
  4. <div></div>
  5. )
  6.  
  7. // ✅ This is perfectly legal because there is just one enclosing tag and
  8. // it can have as many children as it likes
  9. return (
  10. <div>
  11. <div>
  12. <div></div>
  13. </div>
  14. <div></div>
  15. </div>
  16. )
  17.  
  18. // ✅ If you don't want to wrap your component with some enclosing tag like `div`
  19. // you can wrap everything with `React.Fragment` which is a empty tag provided by React
  20. return (
  21. <React.Fragment>
  22. <div></div>
  23. <div></div>
  24. </React.Fragment>
  25. )

Going back to the company profile example, the first child of the enclosing div is another div. Inside that div we used curly braces to display the ticker alongside Profile of:. Remember curly braces is how we inject Javascript code inside JSX. So here the ticker variable would be evaluated and rendered inside that div tag. Then we have another div as a second children of the enclosing parent. Inside this div we again have curly braces and we executed some Javascript code. In this case we mapped each key of the companyProfileInfo object to a div element. The content of this div is again evaluated using another curly braces like: {key} : {companyProfileInfo[key]}. What we did here is told React that for each key of the companyProfileInfo object we want to render a div whose content would be the key followed by a colon : followed by corresponding value for the key on the object (companyProfileInfo[key]).

Let's write some code here to hit the nail on the head. Please open the exercise file and follow the instructions.

Key takeaways:

  • Components must return only one tag. This tag can have as many children as it likes. Instead of a tag, it can however return a string or null.
  • You can run any Javascript code inside the return using curly braces {//run any Javascript}.
  • Outside of the return it's exactly like any other Javascript class or function. You can do whatever you desire to do.

Differences with HTML

There are some commonly used things that are slightly different in JSX than in HTML.

  • StylesIn HTML, styles are passed as string. The css properties are kebab-cased.
  1. <div style="bottom-border: 1px solid green"></div>

In JSX, styles are passed as an object. The css properties are camelCased.

  1. <div style={{ bottomBorder: `1px solid green`}}></div>
  • ClassIn HTML class attribute is passed as string.
  1. <div class="container"></div>

In JSX also class attribute is passed as string but instead of calling it class we call it className. That's because JSX is extension of Javascript and "class" is a reserved keyword in Javascript.

  1. <div className={"container"}></div>
  • Event HandlerIn HTML event handler attribute is all lower cased and the handlers are passed as string.
  1. <div onclick="clickHandler()"></div>

In JSX, event handler are camelCased and instead of string we pass the actual function.

  1. <div onClick={function(){ alert('clicked')}}></div>

We will look more into event handler later in the tutorial.