preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.71k stars 91 forks source link

Does signal support fine-grained prop updates in the React? #404

Closed yumjs closed 1 year ago

yumjs commented 1 year ago
const style = signal({ background: 'red' });

setTimeout(() => {
  // Is it possible to skip the re-rendering of the component <Box> and update the DOM directly?
  style.value = { background: 'green' };
}, 3000);

function Box() {
  return <p style={style}>123</p>;
}
rschristian commented 1 year ago

No, unfortunately that's a limitation of React in that it doesn't give access to what we'd need to support that behavior as we do in Preact.

yumjs commented 1 year ago

@rschristian Does this workaround have potential issues?

const background = signal('red');

setTimeout(() => {
  background.value = 'green';
}, 3000);

function Box() {
  console.log('render');
  const ref = useRef(null);
  useEffect(() => 
    effect(() => {
      if (ref.current) {
        ref.current.style.background = background.value;
      }
    })
  , []);
  return <p ref={ref} style={{ background: background.peek() }}>123</p>;
}
rschristian commented 1 year ago

I don't think that will run into any issues, no.

Up to you whether that's worth it or not.

rschristian commented 1 year ago

Going to close this out as it should be addressed.