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
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 just useEffect. 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 effect useSomething(id). Result of useSomething we wont push to store.
So what we have:
import{useEffect,useRef}from'react';importstorefrom'Services/store';import{callSomeApi}from'Api/something';exportdefaultfunctionuseSomething(){constlastId=useRef()useEffect(async()=>{returnstore.subscribe(({ id })=>{if(lastId.current!==id){lastId.current=id;constresultawait=callSomeApi();store.setState({something: result});}});},[]);}
Of course better if we use action instead store.setState so i add my custom dispatch
// in store.jsexportfunctiondispatch(action){constboundedAction=store.action(action);return(...args)=>boundedAction(...args);}
Now we can do next
import{useEffect,useRef}from'react';importstore,{actions}from'Services/store';exportdefaultfunctionuseSomething(){constlastId=useRef()useEffect(async()=>{returnstore.subscribe(({ id })=>{if(lastId.current!==id){lastId.current=id;dispatch(actions.setSomething)(id);}});},[]);}
Still pretty much code.
But let's imagine that connect takes effect.
It would be cool if we could do this!
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.
As a result, I get an error:
Invalid hook call. Hooks can only be called inside of the body of a function component.
My attempt:
exportfunctionconnectEffect(mapStateToProps,actions){if(typeofmapStateToProps!=='function'){mapStateToProps=select(mapStateToProps||[]);}constboundActions=actions ? mapActions(actions,store) : { store };returnEffect=>{returnownProps=>{constupdate=state=>{constmapped=mapStateToProps(state,ownProps={});for(letiinmapped)if(mapped[i]!==state[i]){state=mapped;returntriggerEffect(state);}for(letiinstate)if(!(iinmapped)){state=mapped;returntriggerEffect(state);}};constunsubscribe=store.subscribe(update);letclearEffect=()=>{};consttriggerEffect=state=>{clearEffect=Effect({ ...ownProps, ...boundActions, ...state});};return()=>{unsubscribe();clearEffect();};};};}
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: