You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -36,14 +37,15 @@ inside our src folder. Then copy the following contents:
36
37
37
38
There are a couple of things to note in our first component:
38
39
39
-
- Our component name, `HelloWorld`, is title-cased (sometimes referred to as _Pascal_ case), with _*H*ello_ and _*W*orld_ capitalized.
40
-
This helps us differentiate components from other JavaScript functions.
40
+
- Our component name, `HelloWorld`, is title-cased (sometimes referred to as _Pascal_ case), with _*H*ello_ and _*W*orld_ capitalized.
41
+
This helps us differentiate components from other JavaScript functions.
41
42
- Our component returns something that looks a lot like HTML, but isn't—it's _JSX_.
42
43
43
44
<FrameworkAsideframework="react">
44
45
While Solid components look like React components, they differ in a few distinct ways. Solid component functions _only run once_ to set up and never run again. If you're used to placing a calculation in the function body of a React component and expecting it to recompute when the component re-renders, you may need to take a different approach with Solid. Don't worry— we'll cover all the best practices throughout this tutorial.
45
46
46
47
Like React, you can include as many components as you like within a file. You can even write a component _inside_ of another!
48
+
47
49
</FrameworkAside>
48
50
49
51
<FrameworkAsideframework="vue">
@@ -57,12 +59,13 @@ In Solid, you can include as many components as you like within a file. You can
57
59
</FrameworkAside>
58
60
<FrameworkAsideframework="svelte">
59
61
60
-
In Svelte, components have _lifecycles_ that are important to how you work with them.
62
+
In Svelte, components have _lifecycles_ that are important to how you work with them.
61
63
In Solid, components are simply functions that group your code. They
62
64
don't have lifecycles, and Solid's rendering system is based on the _reactive_ features you use—we'll get
63
65
to that!
64
66
65
67
Svelte uses Single-File Components, but Solid does not. You can include as many components as you like within a file. You can even write a component _inside_ of another!
68
+
66
69
</FrameworkAside>
67
70
<FrameworkAsideframework="angular">
68
71
In Angular, components have _lifecycles_ that are important to how you work with
@@ -95,13 +98,13 @@ Then, let's replace the boilerplate in `App.tsx` with code that imports and disp
95
98
We used our component just like any other element: after importing the `HelloWorld` function, we can now use `<HelloWorld />`.
96
99
97
100
<Aside>
98
-
By building with components, we're able to separate and reuse code. Components let us
99
-
create building blocks that we can put together to form new pieces.
101
+
By building with components, we're able to separate and reuse code. Components
102
+
let us create building blocks that we can put together to form new pieces.
100
103
</Aside>
101
104
102
105
## What is JSX?
103
106
104
-
As we learned above, Solid components return JSX. JSX is an HTML-like syntax used in Solid JavaScript code to represent part of the Document Object Model (DOM).
107
+
As we learned above, Solid components return JSX. JSX is an HTML-like syntax used in Solid JavaScript code to represent part of the Document Object Model (DOM).
105
108
In our <IfTSfallback="HelloWorld.jsx">HelloWorld.tsx</IfTS> example above, we returned the following JSX from our component:
106
109
107
110
```tsx
@@ -183,6 +186,7 @@ This syntax of binding HTML and attribute values are two different syntaxes in V
183
186
```html
184
187
<div:style="style">Hello {{name}}!</div>
185
188
```
189
+
186
190
</FrameworkAside>
187
191
188
192
Let's update our initial `HelloWorld` component with what we learned in our JSX exploration:
@@ -210,7 +214,7 @@ But the following is _not_ valid JSX because there is no single containing eleme
210
214
<HelloWorld />
211
215
```
212
216
213
-
However, we may not actually want a `div` surrounding this code when it's rendered to the DOM.
217
+
However, we may not actually want a `div` surrounding this code when it's rendered to the DOM.
214
218
_JSX fragments_ give us a way to write valid JSX and also not actually render a containing element to the DOM:
215
219
216
220
```tsx
@@ -232,24 +236,27 @@ _JSX fragments_ give us a way to write valid JSX and also not actually render a
232
236
### JSX is compiled
233
237
234
238
JSX is syntax on top of JavaScript and is not directly part of any JavaScript standard. Therefore, JSX can't be run directly in the browser (or most other JavaScript runtimes). Solid and other frameworks that use JSX require their code to be run through a compiler. If you're using a starter template you don't have to worry about this—everything will just work out of the box.
235
-
239
+
236
240
<Asidetype="note">
237
-
Remember how we discussed that component function names are title-cased? This is so the compiler understands the difference between native DOM elements (like `div`) and custom components (like `HelloWorld`).
241
+
Remember how we discussed that component function names are title-cased? This
242
+
is so the compiler understands the difference between native DOM elements
243
+
(like `div`) and custom components (like `HelloWorld`).
238
244
</Aside>
239
245
240
246
<Asidetype="advanced"collapsibletitle="Read more about the compiler">
241
247
Solid's JSX compiler doesn't just compile JSX to JavaScript; it also extracts reactive values (which we'll get to later in the tutorial) and makes things more efficient along the way.
242
248
243
-
This is more involved than React's JSX compiler, but much less involved than something like Svelte's compiler. Solid's compiler doesn't touch your JavaScript, only your JSX.
249
+
This is more involved than React's JSX compiler, but much less involved than something like Svelte's compiler. Solid's compiler doesn't touch your JavaScript, only your JSX.
250
+
251
+
If you're curious about how Solid and other frameworks use compilation, check out [A Look at Compilation in JavaScript Frameworks](https://dev.to/this-is-learning/a-look-at-compilation-in-javascript-frameworks-3caj) by the creator of Solid.
244
252
245
-
If you're curious about how Solid and other frameworks use compilation, check out [A Look at Compilation in JavaScript Frameworks](https://dev.to/this-is-learning/a-look-at-compilation-in-javascript-frameworks-3caj) by the creator of Solid.
246
253
</Aside>
247
254
## Mounting Solid to the DOM
248
255
249
256
How does our `App` component make it on to the actual HTML page that we show our users?
250
257
251
-
252
258
To add a Solid application to an HTML document, we first designate a containing element in our HTML and assign it an `id`. That usually looks like this:
259
+
253
260
```html
254
261
<body>
255
262
<divid="root"></div>
@@ -259,11 +266,13 @@ To add a Solid application to an HTML document, we first designate a containing
259
266
Then, we call Solid's `render` function, passing in our `App` component and this element:
See how this plays out in our project in the `index.html` file, which creates the div, and <IfTSfallback="src/index.jsx"code>src/index.tsx</IfTS>, which calls that `render` function:
@@ -273,27 +282,27 @@ See how this plays out in our project in the `index.html` file, which creates th
You'll want to make sure the first argument passed to the render function is a function that returns a component `() => <Component />` rather than just the component itself.
297
+
You'll want to make sure the first argument passed to the render function is a function that returns a component `() => <Component />` rather than just the component itself.
290
298
291
299
</Aside>
292
300
293
301
<FrameworkAsideframework="react">
294
302
React defaults to having a `StrictMode` component added to the root of your app, which [changes the render behavior of your code depending on whether you're in a development mode or not](https://beta.reactjs.org/learn/keeping-components-pure#side-effects-unintended-consequences:~:text=Detecting%20impure%20calculations%20with%20StrictMode).
295
303
296
304
Solid has no such behavioral differences between production and development rendering.
305
+
297
306
</FrameworkAside>
298
307
299
308
## Composing multiple components together
@@ -326,7 +335,11 @@ Next, we create an `AddBook` component. This will be a form that eventually allo
326
335
/>
327
336
328
337
Finally, we can _compose_ our `BookList` and `AddBook` components together. Let's create a `Bookshelf` component within
329
-
<IfTScodefallback="App.jsx">App.tsx</IfTS>:
338
+
339
+
<IfTScodefallback="App.jsx">
340
+
App.tsx
341
+
</IfTS>
342
+
:
330
343
331
344
<CodeTabs
332
345
js={[
@@ -390,12 +403,9 @@ function Bookshelf({ name }) {
390
403
Thus far, we learned about Solid components, JSX, and built the foundation of a handy bookshelf app. Next, we'll bring our bookshelf to life using Solid's reactive system!
0 commit comments