You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Besides, when passing a value to another component, it should be wrapped in a field such as value. Can Bistate automatically wrap an atomic type to a value, so we can make the state more clear and readable?
functionInput({ value }){constonChange=useMutate(e=>{value.value=e.target.value;})return<inputvalue={value.value}onChange={onChange}/>}functionApp(){conststate=useBistate({foo: {value: 'some value'}})// expect { foo: 'some value' } but must be { foo: { value: 'some value' }}return<Inputvalue={state.foo}/>}
I have no idea how to update state.foo from the child component to the parent component, since the child component just receives a string.
functionInput({ value }){constonChange=useMutate(e=>{value.value=e.target.value;})return<inputvalue={value.value}onChange={onChange}/>}functionApp(){conststate=useBistate({foo: 'some value'})// state.foo is a string, if we wrap state.foo inside Input, how to notify App?return<Inputvalue={state.foo}/>}
I suggest computed state via useMemo.
functionInput({ text }){constonChange=useMutate(e=>{text.value=e.target.value;})return<inputvalue={text.value}onChange={onChange}/>}functionApp(){constfoo=useBistate({value: 'some value'});constbar=useBistate({value: 'other value'});// state is a plain javascript object// do not mutate it// treat it as read only// useMemo guarantee the state always fresh when deps list changedconststate=useMemo(()=>{return{foo: foo.value,bar: bar.value}},[foo,bar])useEffect(()=>{// use state to do what you want},[])return<Inputvalue={foo}/>}
How about using a special proxy key to wrap the atomic value?
functionInput({ value }){constonChange=useMutate(e=>{value.value=e.target.value;})return<inputvalue={value.value}onChange={onChange}/>}functionApp(){conststate=useBistate({foo: 'some value'})// state.$foo for { value: 'some value' } and state.foo for 'some value'// when 'some value' changes in `Input` component, update state.fooreturn<Inputvalue={state.$foo}/>}
How about using a special proxy key to wrap the atomic value?
functionInput({ value }){constonChange=useMutate(e=>{value.value=e.target.value;})return<inputvalue={value.value}onChange={onChange}/>}functionApp(){conststate=useBistate({foo: 'some value'})// state.$foo for { value: 'some value' } and state.foo for 'some value'// when 'some value' changes in `Input` component, update state.fooreturn<Inputvalue={state.$foo}/>}
Maybe it can make sense, but we must find a good way to support this pattern.
Instead of adding extra property key, binding(bistate, key) function should be better, just like vue 3.0 reactivity api.
I think the API names should be changed when the features become more general
constfoo=useComputed({get: ()=>state.foo,set: value=>state.foo=value})// orconstfoo=useComputed(()=>state.foo,value=>state.foo=value)// and thenconstuseBinding=(state,key)=>useComputed({()=>state[key],value=>state[key]=value})// we gotconstfoo=useBinding(state,'foo')
@littlehaker Sorry for changing API frequently. But I found we maybe go too far away from our original purpose.
useComputed accepts get/set function in function-component, it is too powerful to keep data consistent.
It's hard to prevent users write code like below. Tracing multiple bistate in computed causes updating state indirectly, and less reasonable for now. We need more research to introduce them.
Your newly revised API is good for objects, but its power is limited for data transform. For example:
constfahrenheit=useBistate({value: 10});constcelsius=useComputed({get: ()=>fToC(fahrenheit.value),set: (val)=>fahrenheit.value=cToF(val),});// Cannot be done with binding
To avoid tracing multiple bistate while keeping the power, we may still offer a method to provide common Get/Set operation. Like below:
constfoo=binding(state).foo// is short forconstfoo=binding(state,{get: s=>s.foo,set: (val,s)=>s.foo=val,})constcelsius=binding(fahrenheit,{get: (f)=>fToC(f),set: (c,f)=>f.value=cToF(c),});
letcelsius=useComputed({get: ()=>({value: fToC(fahrenheit.value)}),set: ({ value })=>{fahrenheit.value=cToF(value)}},[fahrenheit.value])
It's easy and natural to handle multiple fields
constcelsius=useMemo(()=>{letcomputed={getvalue(){returnfToC(fahrenheit.value)},setvalue(value){fahrenheit.value=cToF(value)},// add other filedsgettext(){return`${computed.value}°`}}returncomputed},[fahrenheit.value])
Activity
Lucifier129 commentedon Sep 23, 2019
I have no idea how to update state.foo from the child component to the parent component, since the child component just receives a string.
I suggest
computed statevia useMemo.littlehaker commentedon Sep 23, 2019
How about using a special proxy key to wrap the atomic value?
Lucifier129 commentedon Sep 23, 2019
Maybe it can make sense, but we must find a good way to support this pattern.
Instead of adding extra property key,
binding(bistate, key)function should be better, just like vue 3.0 reactivity api.Lucifier129 commentedon Sep 23, 2019
Let's think about it.
PR is welcome!!
Lucifier129 commentedon Sep 23, 2019
I am working in the process for
bindingfeatures now~Lucifier129 commentedon Sep 24, 2019
@littlehaker
I want to know your feeling about the
bindingAPI below, it's ok to you or not?littlehaker commentedon Sep 24, 2019
It's OK for this case.
But maybe we can do a more common design, use a Get/Set pair to specify the binding rule, and make some shorthands.
Lucifier129 commentedon Sep 24, 2019
@littlehaker It's good, it looks even better if we use hooks
Lucifier129 commentedon Sep 24, 2019
I think the API names should be changed when the features become more general
littlehaker commentedon Sep 24, 2019
👍 Good for that.
Lucifier129 commentedon Sep 25, 2019
@littlehaker Sorry for changing API frequently. But I found we maybe go too far away from our original purpose.
useComputedaccepts get/set function in function-component, it is too powerful to keep data consistent.It's hard to prevent users write code like below. Tracing multiple bistate in
computedcauses updating state indirectly, and less reasonable for now. We need more research to introduce them.I propose a new pattern for
bindingAPI, non-selector-based, no hacky property like $foo.Looking forward to your feedback!
littlehaker commentedon Sep 25, 2019
@Lucifier129
The idea of Get/Set comes from use-profunctor-state.
Your newly revised API is good for objects, but its power is limited for data transform. For example:
To avoid tracing multiple bistate while keeping the power, we may still offer a method to provide common Get/Set operation. Like below:
Lucifier129 commentedon Sep 26, 2019
@littlehaker I have tried some implementation, and I figured out that none of them is better than
useMemowithgetter/setter.is better than
It's easy and natural to handle multiple fields
Lucifier129 commentedon Sep 26, 2019
There is still some work we can do, such as handling stale closure, we may introduce
useComputedanduseBindingbased onuseMemoLucifier129 commentedon Sep 27, 2019
@littlehaker useComputed and useBinding are implemented in bistate.
You can click the links above to see how to use.
Give me feedback when you have any question.
We will ship these features in version 1.3.0 later.
littlehaker commentedon Sep 27, 2019
Hi @Lucifier129. It looks really nice, what a great work!
Lucifier129 commentedon Sep 27, 2019
Thx, bistate v1.3.0 has been published to npm, it's available now.