Published
Edited
Oct 29, 2020
Importers
Insert cell
Insert cell
Insert cell
React.render(htm`
<${storeon.StoreContext.Provider} value=${store}>
<${Counter} />
</>
`)
Insert cell
changedStoreState = Generators.observe(next => {
next(store.get())
const unbindEventListerner = store.on("@changed", state => { next(state) })
return () => {
unbindEventListerner()
}
})
Insert cell
store = storeon.createStoreon([
counterModule,
//localStorageModule,
reduxDevtoolsModule,
])
Insert cell
counterModule = store => {
// Initial state.
store.on("@init", () => ({ count: 5, text: "hello" }))
// Reducers returns only changed part of the state.
store.on("more", ({ count, text }, char = "o") => ({
count: count + 1,
text: text + char,
}))
store.on("less", ({ count, text }) => ({
count: Math.max(count - 1, 0),
text: text.slice(0, count - 1),
}))
}
Insert cell
localStorageModule = storeon.persistState(["count", "text"], {
key: "storeon",
storage: window.localStorage,
})
Insert cell
// https://github.com/zalmoxisus/redux-devtools-extension/blob/v2.15.0/docs/API/Arguments.md
reduxDevtoolsModule = storeon.storeonDevtools({
name: "storeon",
})
Insert cell
Counter = () => {
// Counter will be re-render only on `state.count` and `state.text` changes.
const { dispatch, count, text } = storeon.useStoreon("count", "text")
return htm`
<${Button} onClick=${() => dispatch("less")}>less</>
<${Button} onClick=${() => dispatch("more", "o")}>more</>
<${Text}>count: ${count}</>
<${Text}>text: ${text}</>
`
}
Insert cell
Button = styled.button`
font-size: 15px;
`
Insert cell
Text = styled.span`
font-size: 15px;
padding-left: 15px;
`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more