Comments (6)
what you want is coming in version 2.
see code samples here: https://hookstate.js.org/docs/nested-state
from hookstate.
is there a better way that is still strongly typed to do something along the lines of:
gUser.user.hobbies.set(...
The very initial prototype version was something along these lines. There were problems with it: 1. Hookstate link properties collided with user's object properties. 2. It was not possible to use this approach for states with primitive values.
There is a possible improvement - technically it is possible to restore StateLink from a value object (would not work for primitive values). It is possible to develop restoreStateLink
function, which would allow to do something like this:
restoreStateLink(gUser.value.user.hobbies).set(...)
But I am not convinced it is valuable improvement. What do you think?
from hookstate.
Also, in the guide for .set() it mentions a plugin called Mutate, but it's not listed in the plugin section nor in the docs.
This plugin will be deprecated when I publish the updated docs for Hookstate. merge
and set
functions are so powerful, that Mutate is not needed anymore. You can read new docs in https://github.com/avkonst/hookstate/tree/master/hookstate.js.org/docs2
. Just run npm start
in this folder and check nested state section. It explains all clear, I think.
Let me know if newer docs are going in the right direction.
from hookstate.
I am not sure how the reference is checked to trigger the re-render, but is it possible to just set the whole object?
It is possible
will hookstate figure out what has changed?
No, it will not. It will assume the whole object is changed. There are 2 reasons. 1. this is a trade off for extreme performance. 2. it does not require Hookstate to invent object comparison function.
Provided that you can set deeply nested properties by walking the state tree and setting individual properties, I believe this is not a disadvantage that Hookstate does not figure it out.
However, it is possible to add set
method to StateLink which would accept the second argument, which would be a callback function for comparing state values recursively (using visitor pattern or alike). In this case, it would be possible to discover changed parts and re-render efficiently, but the state inspection would be slower. And it would be user's choice what to do. Would it be useful?
from hookstate.
Is it possible to simplify the API to be more functional programming like? This is obviously a bigger change but the API would behave like so:
// func(subject, value)
set(gUser.address.city, 'san antonio');
set(gUser, {some user object})
merge(gUser, {new user object})
we can get the benefits of FP like function currying and I think it will simplify the dev ergonomics, as there is a clear separation between user space and hook state's space.
Currently, the API feels awkward because it has this mutative nature of using nested.property1.set()
, injecting functions into the user object but your comment:
However, it is possible to add
set
method to StateLink which would accept the second argument, which would be a callback function for comparing state values recursively (using visitor pattern or alike). In this case, it would be possible to discover changed parts and re-render efficiently, but the state inspection would be slower. And it would be user's choice what to do. Would it be useful?
This sounds like an immutable approach, I am not sure if it fits with the hookstate's paradigm.
from hookstate.
set(gUser.address.city, 'san antonio');
What would be the type of gUser.address.city
in this example?
from hookstate.
Related Issues (20)
- Any showcase how to work with Immutables? HOT 2
- Resource leak with `useHookstate` HOT 2
- Is this lib still being updated? HOT 3
- Request: throw error when trying to assign nested state to a Proxy object HOT 2
- Migration from 3 to 4 HOT 4
- Localstored - optional initializer HOT 1
- https://hookstate.js.org/docs/exceptions/#hookstate-106 is killing me HOT 1
- What is the meaning of `E` in State<S, E>? HOT 1
- Injecting React-Router-dom instance into the store HOT 1
- one of my favourite projects really HOT 4
- More than 5 extensions loading HOT 2
- Creating a global object using hookstate, but unable to find the property when using it. HOT 2
- How to handle file objects in v4? HOT 1
- Isolating global states between tests HOT 4
- state.attach is not a function
- Combine extensions HOT 3
- Nextjs: localstorage is not defined HOT 6
- having problem with getting rid of proxy HOT 2
- Broadcasted state extension not working HOT 2
- 106 EXCEPTION 'Cannot Convert Undefined Value to Object' Error when setting and getting global store state in two seperate components HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hookstate.