qwik icon indicating copy to clipboard operation
qwik copied to clipboard

[Tutorial] Use of Deprecated Functions useRef()

Open geefuoco opened this issue 3 years ago • 1 comments

I am working through the tutorial and I noticed that useRef is used in multiple places. The app is declaring it as deprecated, and should be replaced with useSignal.

geefuoco avatar Oct 19 '22 23:10 geefuoco

While waiting for an update of the tutorial, here is the solution to use useSignal instead of useRef.

import { component$, useSignal, useClientEffect$, useStore } from '@builder.io/qwik';

export const App = component$(() => {
  const store = useStore({
    width: 0,
    height: 0,
  });
  const outputRef = useSignal<Element>();
  useClientEffect$(() => {
    if (outputRef.value) {
      const rect = outputRef.value.getBoundingClientRect();
      store.width = Math.round(rect.width);
      store.height = Math.round(rect.height);
    }
  });

  return (
    <div>
      <div style={{ border: '1px solid red', width: '110px' }} ref={outputRef}>
        Change text value here to stretch the box.
      </div>
      <div>
        The above red box is {store.height} pixels high and {store.width} pixels wide.
      </div>
    </div>
  );
});

forresst avatar Oct 24 '22 13:10 forresst

@geefuoco @forresst opened a first tutorial update draft. please let me know if anything is missing 🙏

zanettin avatar Nov 07 '22 22:11 zanettin