choo-component
v0.2.2
Published
A super helpful library to imitate reusable stateful components in Choo.
Downloads
4
Readme
Choo component
A super helpful library to imitate reusable stateful components in Choo.
Usage
Creating a component
// titleComponent.js
const component = require('./index')
const titleComponent = component({
model: {
namespace: 'title',
state: {
title: 'Default title'
},
reducers: {
updateTitle (state, payload) {
return {
title: payload.title
}
}
}
},
render (props) {
return html`
<div>
<input
value=${props.title}
oninput=${(e) => {
props.somethingHappened(e.target.value)
props.updateTitle({
title: e.target.value
})
}}
>
${props.title}
</div>
`
}
})
A component's definition must include a model which is an object containing the following.
namespace
(required) - a unique namespace to store instances of this component in your choo global statestate
- default state for an instance of a componentreducers
(required) - an object containing methods to update the internal state of the component. Note that reducers receive current instance's state in it's first argument, and must return updated state for the instanceeffects
- an object containing effects. You will notice that the payload for effects will have aninstanceId
key. Don't worry about it, this is used internally to figure out which component you are updating
A component's definition must also include a view. The view receives a props object that contains a flattened object of the following:
- Current state of the instance
- Methods that correspond to reducer methods
- Props passed from the parent component
When calling reducers from the view, you must pass an object as the payload or bad things will happen!
The above example will export an object containing model and a component. See the sections below for useage.
Using a component in your app
First, register the model in your app.
// app.js
const titleModel = require('./titleComponent').model
app.model(titleModel)
For views, you first need to instantiate the component by passing state
, prev
and send
to it. Next, create an instance of a component by calling the result of the instantiation. An instance expects the following:
- A unique identifier for the component. This needs to be completely unique, and should not change between re-renders of your view. It's how the component knows which slice of state it belongs to
- An optional object containing the following:
2.1
props
- external properties you would like to propagate to your componentsview
. This is useful for callbacks or component customisation 2.2initialState
- this is generally not used, but can be used to overridestate
from the model during the setup stage of a component. Alternatively, you can manually call the reducer from outside your component to achieve the same result
// app.js
const html = require('choo/html')
const titleComponent = require('./titleComponent').component
const mainView = (state, prev, send) => {
const title = titleComponent.component(state, prev, send)
return html`
<div>
<h2>Title component demo</h2>
${title('one', {
props: {
somethingHappened (title) {
console.log('Title one updated', title)
}
}
})}
<br />
${title('two', {
props: {
somethingHappened (title) {
console.log('Title two updated', title)
}
},
initialState: {
title: 'Easy!'
}
})}
</div>
`
}
Note that your component must be initiated in a page that has access to state, prev and send
Altogether now
const choo = require('choo')
const html = require('choo/html')
const component = require('./index')
const app = choo()
const titleComponent = component({
model: {
namespace: 'title',
state: {
title: 'Default title'
},
reducers: {
updateTitle (state, payload) {
return {
title: payload.title
}
}
}
},
render (props) {
return html`
<div>
<input
value=${props.title}
oninput=${(e) => {
props.somethingHappened(e.target.value)
props.updateTitle({
title: e.target.value
})
}}
>
${props.title}
</div>
`
}
})
app.model(titleComponent.model)
const mainView = (state, prev, send) => {
const title = titleComponent.component(state, prev, send)
return html`
<div>
<h2>
Title component demo
</h2>
${title('one', {
props: {
somethingHappened (title) {
console.log('Title one updated', title)
}
}
})}
<br />
${title('two', {
props: {
somethingHappened (title) {
console.log('Title two updated', title)
}
},
initialState: {
title: 'Easy!'
}
})}
</div>
`
}
app.router({ default: '/' }, [
['/', mainView]
])
document.body.appendChild(app.start())
Running the Examples
npm install
/yarn install
npm run dev
- visit http://localhost:8888/