Table of Contents
Ever struggled having to keep your page and your forms synced with a JavaScript data structure? When chaging a piece of data, you have to call this and that function to keep your page up to date with it.
If you are not already using a popular framework dealing with reactivity, you may be considering re-writing your app with one of these.
"Reactive" is a small and standalone library that can introduce reactivity on any project.
Without reactivity:
When A is updated, update C
When B is udpated, update C
The updated value of C is A+B
With reactivity:
The updated value of C is A+B
Reactive will read your function, understand that C is depends on A and B, and will thus update C whenever A or B is updated.
Without Reactive:
function updateFirstname (firstname) {
profile.firstname = firstname;
document.getElementById('profile-firstname').innerText = firstname;
whenEitherUpdated()
}
function updateLastname (lastname) {
profile.lastname = lastname;
document.getElementById('profile-lastname').innerText = lastname;
whenEitherUpdated()
}
function whenEitherUpdated () {
document.getElementById('profile-fullname').innerText = profile.firstname + ' ' + profile.lastname
}
updateFirstname('John');
With Reactive:
reactive.defineWorks([
(prop) => document.getElementById('profile-firstname').innerText = prop.firstname,
(prop) => document.getElementById('profile-lastname').innerText = prop.lastname,
(prop) => document.getElementById('profile-fullname').innerText = prop.firstname + ' ' + prop.lastname,
])
reactive.prop.firstname = 'John'
Full demo and playground : https://codesandbox.io/s/reactive-demo-p30xwl?file=/src/index.js
It is an object property that is constantly observed. When edited, every Works that make use of this Prop will be re-run
Computed properties are built and updated automatically from Props. Each Computed relies on a function with no side effect that uses one or severals props to compute a new value
Functions with side effects. You can use a Work to update Props or do whatever you want. When a Prop used by a Work is modified, the Work will be re-run.
const reactive = new Reactive();
reactive.defineProps({
firstname: 'John',
lastname: 'Doe',
company: {
name: 'Acme'
}
});
reactive.defineFormProps({
// Standard inputs & selects
firstname: { element: firstnameInput },
// Using a custom library with custom getters/setters
customSelect: {
element: customSelect,
accessors: {
getter: (select) => {
return getMySelectValue(select)
},
setter: (select, value) => {
setMySelectValue(select, value)
}
}
}
})
reacitve.defineComuteds({
fullname: (prop) => prop.firstname + ' ' + prop.lastname
})
reactive.defineWorks([
// Short syntax
(prop, computed) => document.getElementById('my-name').innerText = computed.fullname,
// Descriptive syntax
function syncFullname (prop, computed) {
document.getElementById('my-name').innerText = computed.fullname
}
])
const reactive = new Reactive<{
prop1: string
}, {
computed1: string
}>()