As far as I understand, the update function of the useState hook should run in a batch and not re-render the component each time we call it. I have created the following custom hook:
function useObservable(source) {
  const [v, setV] = useState();
  useEffect(() => {
    const subscription = source.subscribe(setV);
    return () => subscription.unsubscribe();
  }, [source]);
  return v;
}
But when I use it multiple times, each emission causes a re-rerender:
const subject = new Subject();
setTimeout(() => {
  subject.next("Value");
}, 1000);
function App() {
  const value = useObservable(subject);
  const value2 = useObservable(subject);
  const value3 = useObservable(subject);
  console.log("render"); <=== called 3 times
  return (
    <div>
      <p>{value}</p>
    </div>
  );
}
Is there a way to optimize this? Have I misunderstood something?
 
    