react#ContextType TypeScript Examples

The following examples show how to use react#ContextType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DOMView.tsx    From react-ecs with MIT License 5 votes vote down vote up
context!: ContextType<typeof EntityContext>;
Example #2
Source File: Entity.tsx    From react-ecs with MIT License 5 votes vote down vote up
context!: ContextType<typeof ECSContext>;
Example #3
Source File: Facet.ts    From react-ecs with MIT License 5 votes vote down vote up
context!: ContextType<typeof EntityContext>;
Example #4
Source File: index.tsx    From react-ecs with MIT License 5 votes vote down vote up
context!: ContextType<typeof EntityContext>;
Example #5
Source File: usePageData.ts    From Hybooru with MIT License 5 votes vote down vote up
export function usePageDataInit(initialData: any): ContextType<typeof PageDataContext> {
  if(initialData._error) initialData = null;
  const history = useHistory();
  const [{ locationKey, pageData }, setPageData] = useState({ locationKey: history.location.key, pageData: initialData || null });
  const [fetching, setFetching] = useState(false);
  const fetchEmitter = useRef<FetchEmitter | null>(null);
  const titleCache = useRef<Record<string, string>>({});
  
  useEffect(() => {
    titleCache.current[history.location.pathname] = document.title;
    
    return history.listen(location => {
      if(fetchEmitter.current?.key && fetchEmitter.current.key !== location.key) {
        fetchEmitter.current?.cancel("Route Change");
        fetchEmitter.current = null;
        setPageData(state => (state.locationKey || state.pageData) ? { locationKey: undefined, pageData: null } : state);
      }
      
      if(titleCache.current[history.location.pathname]) document.title = titleCache.current[history.location.pathname];
    });
  }, [history]);
  
  useEffect(() => {
    if(typeof pageData?._title === "string") {
      document.title = titleCache.current[history.location.pathname] = pageData._title;
    }
  }, [history, pageData]);
  
  const fetch = useCallback(() => {
    if(fetchEmitter.current) {
      fetchEmitter.current.listeners++;
      return fetchEmitter.current.unlisten;
    }
    
    setFetching(true);
    let cancelFetch = () => {};
    requestJSON({
      cancelCb: cancel => cancelFetch = cancel,
    }).then(pageData => {
      setPageData({ locationKey: history.location.key, pageData });
    }).catch(error => {
      console.error("Unable to fetch page data: ", error);
    }).finally(() => {
      fetchEmitter.current = null;
      setFetching(false);
    });
    
    fetchEmitter.current = {
      listeners: 1,
      unlisten() {
        this.listeners--;
        if(this.listeners <= 0) this.cancel("Orphan");
      },
      cancel: cancelFetch,
      key: history.location.key,
    };
    fetchEmitter.current.unlisten = fetchEmitter.current.unlisten.bind(fetchEmitter.current);
    
    return fetchEmitter.current.unlisten;
  }, [history]);
  
  return useMemo(() => ({ pageData, locationKey, fetch, fetching }), [pageData, locationKey, fetch, fetching]);
}