-
-
Notifications
You must be signed in to change notification settings - Fork 89
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
Performance impact: Not re-rendering a component when only calling a shared function #93
Comments
I also tried referring to the solution you proposed for
But here I have another component
The component loads once on the first load and the second due to it being re-rendered since Please help me understand if I am missing something |
…hanges) (#95) Renaming `createUseContext` to `createContextHook`. There are no breaking changes, but it deprecates the current usage of the library (importing default). This will make more sense when we expose more methods (for example, something like `createContextHookArray` that will hopefully address #93).
FYI: I've been working on a solution for this issue. The working in progress is here: https://github.com/diegohaz/constate/tree/feat/multiple-contexts The API looks like this: // renamed to createContextHook, but no breaking changes, since it's default export
import createContextHook from "constate";
function useCounter() {
const [count, setCount] = useState(0);
const increment = () => setCount(count + 1);
return { count, increment };
}
const [Provider, useCount, useIncrement] = createContextHook(
useCounter,
value => value.count, // becomes useCount
value => value.increment // becomes useIncrement
); There are no breaking changes. The current API still works! All these APIs are supported: const useCounterContext = createContextHook(useCounter);
<useCounterContext.Provider />
const useCounterContext = createContextHook(useCounter, value => [value.count]);
<useCounterContext.Provider />
const [CounterProvider, useCounterContext] = createContextHook(useCounter);
<CounterProvider />
<useCounterContext.Provider />
const [CounterProvider, useCount] = createContextHook(useCounter, value => value.count);
<CounterProvider /> |
I see that you have renamed the API which is a step towards addressing this issue. |
Could you elaborate and add an example in the documentation where we can prevent the components from rendering if they are only calling out the functions.
Going with the
count
andIncrement
examples.It is ok to re-render a component which is using count and displaying it if count changes.
But should we really be re-rendering the component which is only calling the
increment
function. The component calling the function does not care what the new state is, all it is interested is in calling the function.More examples on these lines would be great, since I could not find more documentation besides the example on the git page.
Great library and idea. This is exactly what I was looking for, ability to create hooks and then share state globally using context !!! I started doing the work and mid way found your library, so much appreciate it.
The text was updated successfully, but these errors were encountered: