@st-lib/render-with-state
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@st-lib/render element node state management helper

declarative style

import { render, text } from '@st-lib/render'
import { button } from '@st-lib/render-html'
import { onClick } from '@st-lib/render-events'
import { useState } from '@st-lib/render-with-state'

window.onload = () => {
	render(document.body, () => {
		const [state, setState] = useState(0, (newVal, oldVal) => /* increment only */newVal > oldVal)
		button(null, () => {
			onClick(e => {
				e.preventDefault()
				setState(state + 1)
			})
			text(null, 'increment')
		})
		button(null, () => {
			onClick(e => {
				e.preventDefault()
				// will not work because of state guard does not allow new value to be less than old value
				setState(state - 1)
			})
			text(null, 'decrement')
		})
		text(null, ` clicks: ${state}`)
	})
}

functional style

import { render, text } from '@st-lib/render'
import { button } from '@st-lib/render-html'
import { onClick } from '@st-lib/render-events'
import withState from '@st-lib/render-with-state'

window.onload = () => {
	render(document.body, withState(0, (state, setState) => {
		button(null, () => {
			onClick(e => {
				e.preventDefault()
				setState(state + 1)
			})
			text(null, 'click me')
		})
		text(null, ` clicks: ${state}`)
	}))
}

Readme

Keywords

Package Sidebar

Install

npm i @st-lib/render-with-state

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

5.77 kB

Total Files

5

Last publish

Collaborators

  • st-lib