<commit_before>// @flow
import * as React from 'react';

export const ManagerReferenceNodeContext = React.createContext<?Element>();
export const ManagerReferenceNodeSetterContext = React.createContext<
  void | ((?Element) => void)
>();

export type ManagerProps = {
  children: React.Node,
};

export function Manager({ children }: ManagerProps) {
  const [referenceNode, setReferenceNode] = React.useState<?Element>(null);

  React.useEffect(
    () => () => {
      setReferenceNode(null);
    },
    [setReferenceNode]
  );

  return (
    <ManagerReferenceNodeContext.Provider value={referenceNode}>
      <ManagerReferenceNodeSetterContext.Provider value={setReferenceNode}>
        {children}
      </ManagerReferenceNodeSetterContext.Provider>
    </ManagerReferenceNodeContext.Provider>
  );
}<commit_msg>fix(hooks): Avoid setState in unmounted components. (#384)<commit_after>// @flow
import * as React from 'react';

export const ManagerReferenceNodeContext = React.createContext<?Element>();
export const ManagerReferenceNodeSetterContext = React.createContext<
  void | ((?Element) => void)
>();

export type ManagerProps = {
  children: React.Node,
};

export function Manager({ children }: ManagerProps) {
  const [referenceNode, setReferenceNode] = React.useState<?Element>(null);

  const hasUnmounted = React.useRef(false);
  React.useEffect(() => {
    return () => {
      hasUnmounted.current = true;
    };
  }, []);

  const handleSetReferenceNode = React.useCallback((node) => {
    if (!hasUnmounted.current) {
      setReferenceNode(node);
    }
  }, []);

  return (
    <ManagerReferenceNodeContext.Provider value={referenceNode}>
      <ManagerReferenceNodeSetterContext.Provider
        value={handleSetReferenceNode}
      >
        {children}
      </ManagerReferenceNodeSetterContext.Provider>
    </ManagerReferenceNodeContext.Provider>
  );
}
{'fraction_non_alphanumeric': 0.1299254526091587, 'fraction_numerical': 0.001597444089456869, 'mean_word_length': 4.399425287356322, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4773752', 'n_tokens_mistral': 525, 'n_tokens_neox': 507, 'n_words': 102}