-
-
Notifications
You must be signed in to change notification settings - Fork 15
Description
Hello everyone! I want to share my thoughts on transitioning to the new version.
You have all realized that React continues to obscure the internal API, and diving deeper into the reconciler is not very appealing. I feel like a hacker 😁😭. When I started writing this library, I thought about freedom, perfection, and code purity, about minimalism, and that's why use-between turned out great. I hoped the React team would notice and incorporate it into the standard React, but that hasn't happened.
I repeat that I don't want to be a hacker, to go against the system or comply with it. In such cases, I prefer to step out of the game.
Therefore, inspired by the recent dialogue with @bardak-dev, I want to propose a new syntax for use-between. Let's call it "Proposal 2.0".
import React, { useState, useCallback } from 'react';
import { between, Context } from 'use-between';
const useCounter = () => {
const [count, setCount] = useState(0);
const inc = useCallback(() => setCount(c => c + 1), []);
const dec = useCallback(() => setCount(c => c - 1), []);
return {
count,
inc,
dec
};
};
const useSharedCounter = between(useCounter);
const Count = () => {
const { count } = useSharedCounter();
return <p>{count}</p>;
};
const Buttons = () => {
const { inc, dec } = useSharedCounter();
return (
<>
<button onClick={inc}>+</button>
<button onClick={dec}>-</button>
</>
);
};
const App = () => (
<Context>
<Count />
<Buttons />
<Count />
<Buttons />
</Context>
);
export default App;You may notice a few differences from the example on the main page of use-between, but there aren't many. In my opinion, it looks very concise and even better reflects the essence of what's happening.
Now useSharedCounter looks like a true service that is instantiated on demand and can be used in any number of places.
const useSharedCounter = between(useCounter);Also, if you pay attention to the lower part of the code, you will see the emergence of the Context component. Yes, that's correct, this "Proposal 2.0" includes the ability to place the start of the context anywhere, as well as (drumroll) inside the React Router context 🚀, allowing the use of hooks that utilize useContext!
const App = () => (
<Context>
<Count />
<Buttons />
<Count />
<Buttons />
</Context>
);Since this concept differs from what was originally embedded in use-between, I really need the support of my beloved and dear community. Please share your opinion, even if it seems insignificant to you; express it and don't just follow the evaluation of your own judgments! And if you feel your opinion is significant, then all the more reason to share it 🤗. Or simply, some kind words about use-between will be more valuable than gold to me as the author of use-between.
Please feel free to be yourself on this page.
Sincerely yours,
Slava