#Whats the best way in remix and or react to use window.open anchors?

1 messages · Page 1 of 1 (latest)

fickle moon
#

Just in the loaderfunction. Need to open another window to open a pdf file, but then return redirect. So cant just return redirect to the file...

#
    const handle = window.open(process.env.APPLICATION_URL + "/admin/orders/" + order.id + "/label", "_blank");
#

but this results in ```bash
Unexpected Server Error

ReferenceError: window is not defined```

#

which i get, but whats the alternative here? It isnt a view as such.... so useEffect isnt relevant is it?

fringe tulip
#

Upp!

river wren
#

you could get the window object from a useEffect

import { useEffect, useState } from "react";

export const useWindow = () => {
  const [windowInstance, setWindowInstance] = useState<
    typeof window | undefined
  >(undefined);

  useEffect(() => {
    setWindowInstance(window);
  }, []);

  return windowInstance;
};
#

Then you should be able to call

#
const window = useWindow()

In any component to get it

#

but the reason I think it should work is just because remix runs what it can on the backend. If it's in a useEffect it won't run it until it gets to the front