-
Notifications
You must be signed in to change notification settings - Fork 139
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
[feature] Сonnect effect to store #155
Comments
I try to do it myself, but I do not have enough experience to understand how to run a hook in the context of the component.
My attempt: export function connectEffect(mapStateToProps, actions) {
if (typeof mapStateToProps !== 'function') {
mapStateToProps = select(mapStateToProps || []);
}
const boundActions = actions ? mapActions(actions, store) : { store };
return Effect => {
return ownProps => {
const update = state => {
const mapped = mapStateToProps(state, ownProps = {});
for (let i in mapped) if (mapped[i] !== state[i]) {
state = mapped;
return triggerEffect(state);
}
for (let i in state) if (!(i in mapped)) {
state = mapped;
return triggerEffect(state);
}
};
const unsubscribe = store.subscribe(update);
let clearEffect = () => { };
const triggerEffect = state => {
clearEffect = Effect({ ...ownProps, ...boundActions, ...state });
};
return () => {
unsubscribe();
clearEffect();
};
};
};
} |
I think we're going to look into adding a dedicated hooks API for the React and Preact unistore integrations. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello. I want to offer an interesting feature. How about making connect able to take not only a component but also an effect? At first glance it sounds strange, but let's look at the use-cases.
For example, I want to get some data from the API before the application renders some part of the application. There are many ways to do this. HOC with RenderProps where we use
componentDidMount
, or justuseEffect
. If we use useEffect we need to import some methods to initialize the query into API, data conversion ect. It is best to make it a separate effectuseSomething(id)
. Result of useSomething we wont push to store.So what we have:
Of course better if we use action instead
store.setState
so i add my custom dispatchNow we can do next
Still pretty much code.
But let's imagine that connect takes effect.
It would be cool if we could do this!
What do you think about it?
The text was updated successfully, but these errors were encountered: