-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add simple counter javascript example
- Loading branch information
1 parent
3537cde
commit b76994f
Showing
5 changed files
with
61 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,6 @@ | ||
node_modules | ||
dist | ||
build | ||
.DS_Store | ||
.parcel-cache | ||
package-lock.json |
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,8 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script defer type="module" src="./index.js"></script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
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,27 @@ | ||
import { signal, h, Div, H2, Button, derive } from 'doom-reactive-state' | ||
|
||
const App = () => { | ||
// this is a non-reactive component it's out of the renderer loop since it isn't wrapped with the `effect` function | ||
// here we can instantiate the state | ||
const [count, setCount] = signal(0) | ||
|
||
const increment = () => setCount(count() + 1) | ||
|
||
const countText = derive('', () => `${count()}`) | ||
|
||
// only the changed attributes of the component will be updated | ||
return Div({ children: [ | ||
// only functions inside objects listen to the state changes | ||
H2({ className: 'a-class', children: [ | ||
'Count: ', | ||
// all properties or text-children we want to react to changes must be functions | ||
h('span', { children: [countText] }) | ||
]}), | ||
// you can avoid the element reacting for a specific property: see children property, we pass it directly without any function | ||
Button({ onclick: increment, children: ['increment'] }), | ||
]}) | ||
} | ||
|
||
// no need to use magic stuff to attach components to the dom, | ||
// we always return a DOM Element from our components | ||
document.body.appendChild(App()) |
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,15 @@ | ||
{ | ||
"name": "simple-counter-javascript", | ||
"scripts": { | ||
"start": "parcel index.html" | ||
}, | ||
"author": "Alessio Coser", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"parcel": "^2.8.0", | ||
"typescript": "^4.9.3" | ||
}, | ||
"dependencies": { | ||
"doom-reactive-state": "^0.3.1" | ||
} | ||
} |
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,5 @@ | ||
# doom-reactive-state Example: Async count history with typescript | ||
|
||
1. `npm install` | ||
2. `npm start` | ||
3. open `http://localhost:1234` |