dai-shi / use-context-selector

React useContextSelector hook in userland
https://www.npmjs.com/package/use-context-selector
MIT License
2.61k stars 58 forks source link

Questions about "best practices" or NOT to do's #134

Open IrvingArmenta opened 2 weeks ago

IrvingArmenta commented 2 weeks ago

Question

I found this library by looking for a way to enhance/improve the functionality of React.Context So I have some questions about what not to do, best practice usage, etc.

  1. Is there a performance issue if I implement "helper" hooks similar to these:
    
    export const useMainStoreState = <T extends keyof State>(resource: T) => {
    return useContextSelector(context, (v) => v[0][resource]) as State[T];
    };

export const useMainStoreDispatch = () => { return useContextSelector(context, (v) => v[1]); }


2. Is there any performance issue if I setup/access `state` and `dispatch` in "Object" instead of  a "Tuple" ? 
```tsx
export const context = createContext<{ state: State, dispatch: Dispatch }>({ state: initialState, dispatch: () => null });

// then in component  
const resource = useContextSelector(context, (v) => v.state.resource);
const dispatch = useContextSelector(context, (v) => v.dispatch);

// this may also be combined with the code on top about `useMainStore()` 
dai-shi commented 2 weeks ago

1: No 2: No


As useContextSelector RFC is basically rejected, I would generally suggest the following.

This library exists as an internal library for React-Tracked and has been maintained. Its purpose is to make it compatible with Concurrent Rendering, but it's not often the capability people need.