|
| 1 | +# Writing JSX |
| 2 | + |
| 3 | +## Converting HTML to JSX |
| 4 | + |
| 5 | +Here is an example of how you might convert a simple HTML file to JSX: |
| 6 | + |
| 7 | +```html |
| 8 | +<div class="container"> |
| 9 | + <h1>Hello World!</h1> |
| 10 | + <h2>Good to see you here.</h2> |
| 11 | + <img src="https://example.com/favicon.ico" alt="Logo" > |
| 12 | +</div> |
| 13 | +``` |
| 14 | + |
| 15 | +If you put it into JSX, it would create a error. JSX tags must have a closing tag, and the `<img>` tag doesn't have a closing tag. So you need to add a closing tag to the `<img>` tag. |
| 16 | + |
| 17 | +So the JSX version of the above HTML would be: |
| 18 | + |
| 19 | +```js |
| 20 | +<div className="container"> |
| 21 | + <h1>Hello World!</h1> |
| 22 | + <h2>Good to see you here.</h2> |
| 23 | + <img src="https://example.com/favicon.ico" alt="Logo" /> // [!code focus] |
| 24 | +</div> |
| 25 | +``` |
| 26 | + |
| 27 | +## Rules of JSX |
| 28 | + |
| 29 | +### Must return a single element |
| 30 | + |
| 31 | +JSX must have a single root element. If you want to return multiple elements, you need to wrap them in a single element. |
| 32 | + |
| 33 | +Here is a wrong example: |
| 34 | + |
| 35 | +```js |
| 36 | +const element = ( |
| 37 | + <h1>Hello!</h1> |
| 38 | + <h2>Good to see you here.</h2> |
| 39 | +); |
| 40 | +``` |
| 41 | + |
| 42 | +Here is a correct example: |
| 43 | + |
| 44 | +```js |
| 45 | +const element = ( |
| 46 | + <div> |
| 47 | + <h1>Hello!</h1> |
| 48 | + <h2>Good to see you here.</h2> |
| 49 | + </div> |
| 50 | +); |
| 51 | +``` |
| 52 | + |
| 53 | +### Don't want to add a wrapper element? |
| 54 | + |
| 55 | +If you don't want to add a wrapper element, you can use Fragment to wrap multiple elements. |
| 56 | + |
| 57 | +```js |
| 58 | +const element = ( |
| 59 | + <> |
| 60 | + <h1>Hello!</h1> |
| 61 | + <h2>Good to see you here.</h2> |
| 62 | + </> |
| 63 | +); |
| 64 | +``` |
| 65 | + |
| 66 | +`<>` is a shorthand for `Fragment`. It is a common pattern to use Fragment to wrap multiple elements. It is also a good practice to use Fragment when you don't want to add a wrapper element. |
| 67 | + |
| 68 | +```js |
| 69 | +const element = ( |
| 70 | + <Fragment> |
| 71 | + <h1>Hello!</h1> |
| 72 | + <h2>Good to see you here.</h2> |
| 73 | + </Fragment> |
| 74 | +); |
| 75 | +``` |
| 76 | + |
| 77 | +### Must be closed |
| 78 | + |
| 79 | +JSX tags must be closed. If you don't want to add a child element, you can use self-closing tag. |
| 80 | + |
| 81 | +```js |
| 82 | +<div> |
| 83 | + <h1>Hello!</h1> |
| 84 | + <br /> // [!code focus] |
| 85 | + <h2>Good to see you here.</h2> |
| 86 | + <hr /> // [!code focus] |
| 87 | + <img src="https://example.com/favicon.ico" alt="Logo" /> // [!code focus] |
| 88 | +</div> |
| 89 | +``` |
| 90 | + |
| 91 | +### camelCase all HTML attributes (except `class` ) |
| 92 | + |
| 93 | +JSX in jwc.js is different from usual JSX. We have changed some attributes to the original attribute name. |
| 94 | + |
| 95 | +::: warning |
| 96 | +Because jwc.js still in `alpha` status, we may change the attribute name in the future. |
| 97 | +::: |
| 98 | + |
| 99 | +1. camelCase all most of the **event** names. For example, `onclick` becomes `onClick`. |
| 100 | + |
| 101 | +```js |
| 102 | +<button onClick={handleClick}>Click me</button> |
| 103 | +``` |
| 104 | + |
| 105 | +2. `class` remains the same <Badge text="Pending" type="warning" />. It's different from React, which uses `className`. |
| 106 | + |
| 107 | +```js |
| 108 | +<div class="container"></div> |
| 109 | +``` |
0 commit comments