Skip to content

Instantly share code, notes, and snippets.

@sbussard
Last active May 16, 2019 19:27
Show Gist options
  • Save sbussard/b2d4ef97efcb59b8dc65084df90eb4b3 to your computer and use it in GitHub Desktop.
Save sbussard/b2d4ef97efcb59b8dc65084df90eb4b3 to your computer and use it in GitHub Desktop.

Revisions

  1. sbussard revised this gist May 16, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions exampleRef.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    import React, { useState } from "react";
    import ChildComponent from "../etc/somewhere";

    let ChildComponent = ({ setRef }) => <div ref={setRef}>{/* content */}</div>;

    let ParentComponent = () => {
    let [ref, setRef] = useState();
    @@ -10,5 +11,5 @@ let ParentComponent = () => {
    }
    }, [ref]);

    return <ChildComponent ref={setRef} />;
    return <ChildComponent setRef={setRef} />;
    };
  2. sbussard created this gist May 16, 2019.
    14 changes: 14 additions & 0 deletions exampleRef.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    import React, { useState } from "react";
    import ChildComponent from "../etc/somewhere";

    let ParentComponent = () => {
    let [ref, setRef] = useState();

    useEffect(() => {
    if (ref) {
    // whatever you want to use ref for
    }
    }, [ref]);

    return <ChildComponent ref={setRef} />;
    };