Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not update store in useEffect hooks function. #164

Open
JayWang0 opened this issue Jan 4, 2024 · 1 comment
Open

Can not update store in useEffect hooks function. #164

JayWang0 opened this issue Jan 4, 2024 · 1 comment

Comments

@JayWang0
Copy link

JayWang0 commented Jan 4, 2024

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 (


<NumberInput storeKey="one" value={store.get('one')} />
<NumberInput storeKey="two" value={store.get('two')} />
Sum: {store.get('sum')}

);
}

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);

`

@JayWang0
Copy link
Author

JayWang0 commented Jan 4, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant