Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
state-tree
Advanced tools
A state tree that handles reference updates and lets you flush a description of changes
A state tree that handles reference updates and lets you flush a description of changes
There are different ways of handling state. You have libraries like Baobab and Mobx. They are part of the same domain, but they handle state changes very differently. From experience this is what I want:
So here we are. I want a single state tree that has controlled mutations, allowing referencing and emits what changed along with any deps. This is rather low level code that would require abstractions for a good API, but it is a start :-)
tree.js
import StateTree from 'state-tree';
export default StateTree({
list: []
});
addItem.js
import tree from './tree';
export default addItem(item) {
tree.push('list', item);
}
Items.js
import React from 'react';
import HOC from 'state-tree/react/HOC';
import addItem from './addItem';
function Items(props) {
return (
<div>
<button onClick={() => addItem({foo: 'bar'})}>Add item</button>
<ul>
{props.list.map((item, index) => <li key={index}>{item.foo}</li>)}
</ul>
</div>
);
}
export default HOC(Items, {
list: 'list'
})
main.js
import React, {render} from 'react';
import Container from 'state-tree/react/Container';
import tree from './tree';
import Items from './Items';
render((
<Container tree={tree}>
<Items />
</Container>
), document.querySelector('#app'));
import StateTree from 'state-tree';
const tree = StateTree({
foo: 'bar'
});
import StateTree from 'state-tree';
const tree = StateTree({
foo: {
bar: 'value'
}
});
tree.get('foo.bar'); // "value"
import StateTree from 'state-tree';
const tree = StateTree({
foo: 'bar',
list: []
});
tree.set('foo', 'bar2');
tree.unset('foo');
tree.push('list', 'something');
tree.pop('list');
tree.shift('list');
tree.unshift('list', 'someValue');
tree.splice('list', 0, 1, 'newValue');
import StateTree from 'state-tree';
const tree = StateTree({
foo: 'bar',
list: [{
foo: 'bar'
}]
});
tree.set('foo', 'bar2');
tree.flushChanges(); // { foo: true }
tree.set('list.0.foo', 'bar2');
tree.flushChanges(); // { list: { 0: { foo: true } } }
With the flushed changes you decide when it is time to update the interface. You can use this flushed change tree with abstractions in your UI. An example could be:
{ someList: 'foo.bar.list' }
tree.subscribe(function (changes) {
var hasUpdate = listPath.reduce(function (changes, key) {
return changes[key];
}, changes); // undefined
if (hasUpdate) {
component.forceUpdate();
}
})
In this case, if we updated "foo" and the UI registers to "list" it would not update. This logic is instead of having a register of observables. This also make sure that when you for example register to "list" and a change happens on { list: { 0: true } }
the component will still update, which it should.
So with this lib you can have a list of users and just add them to the posts.
import StateTree from 'state-tree';
const userA = {
name: 'john'
};
const tree = StateTree({
users: [userA],
posts: [{
title: 'Some post',
user: userA
}]
});
tree.set('users.0.name', 'woop');
tree.flushChanges(); // { users: { 0: true }, posts: { 0: { user: true } } }
FAQs
A state tree that handles reference updates and lets you flush a description of changes
The npm package state-tree receives a total of 31 weekly downloads. As such, state-tree popularity was classified as not popular.
We found that state-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.