Skip to content

Commit

Permalink
add simple counter javascript example
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioCoser committed Mar 23, 2024
1 parent 3537cde commit b76994f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/simple-counter-javascript/.gitignore
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
8 changes: 8 additions & 0 deletions examples/simple-counter-javascript/index.html
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>
27 changes: 27 additions & 0 deletions examples/simple-counter-javascript/index.js
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())
15 changes: 15 additions & 0 deletions examples/simple-counter-javascript/package.json
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"
}
}
5 changes: 5 additions & 0 deletions examples/simple-counter-javascript/readme.md
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`

0 comments on commit b76994f

Please sign in to comment.