Home / File/ repro-mutate-ref-in-function-passed-to-hook.js — react Source File

repro-mutate-ref-in-function-passed-to-hook.js — react Source File

Architecture documentation for repro-mutate-ref-in-function-passed-to-hook.js, a javascript file in the react codebase.

Entity Profile

Relationship Graph

Source Code

// @flow
component Example() {
  const fooRef = useRef();

  function updateStyles() {
    const foo = fooRef.current;
    // The access of `barRef` here before its declaration causes it be hoisted...
    if (barRef.current == null || foo == null) {
      return;
    }
    foo.style.height = '100px';
  }

  // ...which previously meant that we didn't infer a type...
  const barRef = useRef(null);

  const resizeRef = useResizeObserver(
    rect => {
      const {width} = rect;
      // ...which meant that we failed to ignore the mutation here...
      barRef.current = width;
    } // ...which caused this to fail with "can't freeze a mutable function"
  );

  useLayoutEffect(() => {
    const observer = new ResizeObserver(_ => {
      updateStyles();
    });

    return () => {
      observer.disconnect();
    };
  }, []);

  return <div ref={resizeRef} />;
}

Subdomains

Frequently Asked Questions

What does repro-mutate-ref-in-function-passed-to-hook.js do?
repro-mutate-ref-in-function-passed-to-hook.js is a source file in the react codebase, written in javascript. It belongs to the TestingUtilities domain, Fixtures subdomain.
What functions are defined in repro-mutate-ref-in-function-passed-to-hook.js?
repro-mutate-ref-in-function-passed-to-hook.js defines 2 function(s): resizeRef, updateStyles.
Where is repro-mutate-ref-in-function-passed-to-hook.js in the architecture?
repro-mutate-ref-in-function-passed-to-hook.js is located at compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-mutate-ref-in-function-passed-to-hook.js (domain: TestingUtilities, subdomain: Fixtures, directory: compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free