Open JayWang0 opened 8 months ago
I want to listen to the property change, and then update the store, however it doesn't work. any workaround?
Here is my code: ` import React from 'react'; import Store from './MyStore';
// Re-render the component when the store updates.
function MyComponent() { let store = Store.useStore();
React.useEffect(() => { store.on('one').subscribe((one) => { console.log(store); store.set('sum')(store.get('one') + store.get('two')); });
store.on('two').subscribe((two) => { console.log(store); store.set('sum')(store.get('one') + store.get('two')); });
}, []);
return (
); }
function NumberInput({ storeKey, value }) { let store = Store.useStore();
return ( <input onChange={(e) => store.set(storeKey)(parseInt(e.target.value, 10))} type="number" value={value} /> ); }
export default Store.withStore(MyComponent);
`
` import { createConnectedStore } from 'undux';
let initialState = { one: 0, two: 0, sum: 0, };
export default createConnectedStore(initialState);
https://undux-readme-demo-js-mgnrwt.stackblitz.io
I want to listen to the property change, and then update the store, however it doesn't work. any workaround?
Here is my code: ` import React from 'react'; import Store from './MyStore';
// Re-render the component when the store updates.
function MyComponent() { let store = Store.useStore();
React.useEffect(() => { store.on('one').subscribe((one) => { console.log(store); store.set('sum')(store.get('one') + store.get('two')); });
}, []);
return (
); }
function NumberInput({ storeKey, value }) { let store = Store.useStore();
return ( <input onChange={(e) => store.set(storeKey)(parseInt(e.target.value, 10))} type="number" value={value} /> ); }
export default Store.withStore(MyComponent);
`
` import { createConnectedStore } from 'undux';
let initialState = { one: 0, two: 0, sum: 0, };
export default createConnectedStore(initialState);
`