Hi guys,
coming from React I wonder if it’s possible to write components in similar manner as with React.
I want task component to be stateless. Just render what it gets via item param and after updating the task just call onUpdate. tasks component keeps the state. I do have problem that I do not know how to do it properly. Following code does not work because Template.instance() in helper returns null.
Is there a way to achieve similar behaviour as in React?
Thanks for help
<template name="tasks">
<div>
{{#each task in tasks}}
{{> task item=task onUpdate=onUpdateTask }}
{{/each}}
</div>
</template>
Template.tasks.onCreated(function() {
this.state = new ReactiveDict()
this.state.set('tasks', { 1: { id: 1, name: "Clean house" } })
})
Template.tasks.helpers({
tasks() { return Template.instance().state.get('tasks') },
onUpdateTask() {
const instance = Template.instance() // instance is null in this case
return (updatedTask) => instance.state.set("tasks", { ...instance.state.get("tasks"), [updatedTask.id]: updatedTask });
// need to force rerender of task component since it has no state
}
})