středa 8. ledna 2020

Are React Hooks fundamentaly broken?

I am still not decided if React hooks are as broken as I think or I just don’t get the essence of them, but run into several troubles:


* they introduce completely new paradigm that no one is used to


* I understand why useEffect is using basic Object.is instead of some other more/less clever algorithm, but is it really good idea to put together Object.is (what is basically a reference comparison for non value types) and functional components where everything is fresh new on every render call? IMHO this two things just don’t fit well together


* reusability of hooks in case you want to apply them on multiple items instead of one eg. when you have hook for fetching data from server 
```
useFetch = (url:string) => {….}
```
If you want to download data from 3 endpoints
```
[url1, url2, url3].each(url => useFetch(url))
```
 React will say NO
but if you put it in three components that actually only wraps your useFetch
```
function UseFetchWrapperComponent({url}) => {useFetch(url)}
[url1, url2, url3].map(url => <UseFetchWrapperComponent url={url} />)
```
That is fine. WTF?

* Hooks composition is not easy as well. For examples if you want a hook that computes some derived value from result of other hook. How to do that?
```
const [state, setState] = useState();
const [derived, setDerived] = useState();

useEffect(() => {
  setDerived(computeSomthing(state))
}, [state]);
```
This is really awfull and definitely not scalable.
Compare it to other existing reactive library
```
fromEvent(document, 'click') .pipe( throttleTime(1000), map(event => event.clientX), scan((count, clientX) => count + clientX, 0) ) .subscribe(count => console.log(count));
```

Maybe I just got the whole idea of hooks wrong. I don't know.