@composi/runtime:
Init
Preparing for Startup
When you pass a program to the run
function, the runtime takes the value returned by the init
method to use for setting things up. If your program has state, then the init
method must return it. The state that init returns can be any valid JavaScript data: null, undefined, string, number, Array, Set, Map, Object.
const program = {
init() {
return state
},
view(state, send) {},
update(state, msg, send) {}
subscriptions(send, getState) {}
}
You might be wondering why you should be able to set state as null or undefined. Image that in your program's subscriptions you plan on fetching data from somewhere to use as your program's state. This means you don't really have state to return yet. So you can just set it as null
. Or you could leave the init
method empty. Either way, if you are going to fetch your data in a subscription and leave state undefined, you'll need to check state before trying to render the view. You can use a simply state &&
check before returning the view:
const program = {
init() {
return null
},
view(state, send) {
// Check state first to see if its truthy:
return state && render(<List {...{state, send}}/>, '#list')
},
update(state, msg, send) {
},
subscriptions(send, getState) {
// Fetch some data to use as state:
return fetchDataForState(send, getState)
}
}
Here's a simple Hello World example:
See the Pen @composi/core + runtime Hello World by Robert Biggs (@rbiggs) on CodePen.