JSX allows us to write HTML in React, making it easier to understand what's happening with the components, just by looking at the JSX part of the component. JSX produces React “elements” and we will render them to the DOM.
const message = 'PushDev';
const element = <h1 className='greeting'>Hello, {message}</h1>;
ReactDOM.render(
element,
document.getElementById('root')
);Under the hood it is using React.createElement()
const message = 'PushDev';
const element = React.createElement('h1', {className: 'greeting'}, `Hello, ${message}`);
ReactDOM.render(
element,
document.getElementById('root')
);Because JSX is not JavaScript we will convert it using Babel code compiler. You can get it through the CDN or using npm.
With JSX you can write expressions using curly braces { }. Everything contained inside the curly braces will be evaluated as Javascript code.
const myElement = <h1>I'm { 20 + 1 } years old</h1>;You must use className instead of class. These reserved words are mandatory to avoid clashing with the ones used in HTML.
const myElement = <h1 className='Title'>Hello Push!</h1>;const greeting = () => console.log('Hello Push!');
const myElement = <button onClick={greeting}>Click me!</button>;const src = '../mypath';
const myImage = <img src={src} alt='my image' />;Put the HTML inside parentheses when working with multiple lines.
const myElement = (
<div>
<p>Hello</p>
<p>Push!</p>
</div>
);The HTML code must be wrapped only in ONE top-level element.
const myList = (
<ul className='mylist'>
<li className='mylist__item'>
<p>JavaScript</p>
</li>
<li className='mylist__item'>
<p>Python</p>
</li>
<li className='mylist__item'>
<p>Java</p>
</li>
</ul>
);Note: You can use <></> to wrap elements, it is known as Fragment.
render() {
return (
<>
<Header />
<Navigation />
<Main />
<Footer />
</>
);
}const age = 19;
const myElement = <p>{age < 18 ? "Sorry, you can't enter the bar" : 'Welcome! 🍺'}</p>;- Let's create a BMI (Body Mass Index) calculator.
- In your forked
react-tutorialrepo, create a new branch namedfeature/2-using-jsx - Create a
index.htmlfile within the2-using-jsxfolder. - You will have 2 variables: Height (Meters) and weight (KG). The equation to calculate the BMI is:
- BMI = WEIGHT / HEIGHT 2
- Based on the following table you will get the BMI information.
| BMI | Condition | Description |
|---|---|---|
| underweight | BMI < 18.5 | You should see your doctor for advice on gaining weight in a healthy way. |
| normal | BMI >= 18.5 && BMI < 24.9 | You acquire healthy habits such as balanced eating and physical activity. |
| overweight | BMI >= 24.9 | You should therefore consult your doctor for options for reducing weight and body fat. |
- Based on the BMI you will need to render:
- Title: Element with the title of the app (e.g BMI Calculator).
- Image: Look up into the
assetsfolder, you will need to render the image based on the BMI result. - Description: Look up the table above.
Use the different JSX concepts described in this section.
Bonus
- Use a FORM element to get the weight and height. You can create a button to calculate the BMI.
- Use classes to identify the BMI and add styles for each classification.
- Push your changes and create the Pull request.