figma-plugin-react-template icon indicating copy to clipboard operation
figma-plugin-react-template copied to clipboard

onmessage on App.tsx won't fire if plugin is closed immediately

Open butteredcorn opened this issue 2 years ago • 2 comments

This tripped me up for a while until I realized why the onmessage in App.tsx doesn't fire in the example. Maybe just add a setTimeout to the figma.closePlugin call in controller.ts along with a comment.

butteredcorn avatar Feb 12 '23 03:02 butteredcorn

Hey @butteredcorn, I'd be happy to get a PR fixing this 🙏🏼

nirsky avatar Apr 19 '23 06:04 nirsky

I don't think the state is being updated inside the onmessage callback. The setIsLoading(false) isn't working, yet there is output in the console.

useEffect(() => {
    const messageHandler = (event) => {
          const { type, data } = event.data.pluginMessage;
          switch (type) {
            case FromPlugin.SETTINGS_LOADED:
            case FromPlugin.SETTINGS_SAVED:
              console.log('DATA', data);
              setIsLoading(false);
              console.log('FALSE');
              break;
          }
    };

    window.addEventListener('message', messageHandler.bind(this));

    return () => {
      window.removeEventListener('message', messageHandler);
    };
  }, []);

usman-careem avatar Mar 20 '24 16:03 usman-careem