-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: macdonst <[email protected]>
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
title: A React Developers Guide to Writing Enhance Components | ||
--- | ||
|
||
Frequently, we are asked by React developers why patterns they have learned while writing components using JSX do not translate to writing web components. In this doc, we'll describe some common gotchas that developers coming from React or other JavaScript frameworks may run into when writing plain vanilla web components. | ||
|
||
## String Interpolation | ||
|
||
<code-compare> | ||
|
||
<doc-code filename="JavaScript"> | ||
|
||
```javascript | ||
const element = `<h1>${title}</h1>`; | ||
``` | ||
|
||
</doc-code> | ||
|
||
|
||
<doc-code filename="React"> | ||
|
||
```javascript | ||
const element = <h1>{title}</h1>; | ||
``` | ||
|
||
</doc-code> | ||
|
||
</code-compare> | ||
|
||
## Attribute Quoting | ||
|
||
<code-compare> | ||
|
||
<doc-code filename="JavaScript"> | ||
|
||
```javascript | ||
const image = `<img src="${href}" alt="${altText}" />`; | ||
``` | ||
|
||
</doc-code> | ||
|
||
|
||
<doc-code filename="React"> | ||
|
||
```javascript | ||
const image = <img src={href} alt={altText} /> | ||
``` | ||
|
||
</doc-code> | ||
|
||
</code-compare> | ||
|
||
## Rendering Markup from Arrays | ||
|
||
<code-compare> | ||
|
||
<doc-code filename="JavaScript"> | ||
|
||
```javascript | ||
const todoList = `<ul> | ||
${todos.map((todo) => ( | ||
`<li key="${todo.id}">${todo.text}</li>` | ||
))} | ||
</ul>` | ||
``` | ||
|
||
</doc-code> | ||
|
||
|
||
<doc-code filename="React"> | ||
|
||
```javascript | ||
const todoList = <ul> | ||
{todos.map((todo) => ( | ||
<li key={todo.id}>{todo.text}</li> | ||
))} | ||
</ul> | ||
``` | ||
|
||
</doc-code> | ||
|
||
</code-compare> | ||
|
||
For a more verbose description the different between Enhance and React components [read this post](https://begin.com/blog/posts/2024-03-08-a-react-developers-guide-to-writing-enhance-components). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default function CodeCompare ({ html }) { | ||
return html` | ||
<style> | ||
:host { | ||
display: grid; | ||
grid-template-columns: repeat(2, 1fr); | ||
grid-template-rows: 1fr; | ||
gap: var(--space-0); | ||
width: 100%; | ||
} | ||
</style> | ||
<slot></slot> | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters