microstates
microstates copied to clipboard
Calling transition on child object with `intialize` transition does not return parent microstate
Given a child class with an initialize transition
class Pagination {
page = Number;
itemsPerPage = Number;
initialize() {
let pagination = this;
if (!pagination.page.state) {
pagination = this.page.set(1);
}
if (!pagination.itemsPerPage.state) {
pagination = this.itemsPerPage.set(25);
}
return pagination;
}
}
and a parent class that uses it
class Table {
pagination = Pagination;
}
Calling a transition on the child property returns the child property not the parent property:
table.pagination.page.set(5) // Returns page microstate not table microstate
You can see an example here: https://codesandbox.io/s/y7v1v8op8j
Hi @brandynbennett, thank you for catching another bug for us. I was able to reproduce this in runkit https://runkit.com/taras/initialization-test. It's highlighting area of functionality where we don't have sufficient test coverage.
While we sort this out, you can use create to provide default values.
const { create, valueOf } = require('microstates');
class Pagination {
page = create(Number, 1)
itemsPerPage = create(Number, 25)
}
class Table {
pagination = Pagination
}
let table = create(Table);
table.pagination.page.set(5) instanceof Table
//> true
You can see it working here https://runkit.com/taras/initialization-test-create-default-value
Awesome @taras Thanks for the pointer.
Here is a branch with a failing case for this use case tm/initializing-nested-node
Has there been any progress on this issue?
We're still working on it, but it is definitely a priority. I'd expect a solution in the next two weeks.