Right way to handle executeAsModal in a panel with selection changed event

I am trying to create a type of panel in React (Preact) that changes display when the layer selection changes. The code is already working as intended, but I have a few questions.

  1. According to the documentation, I need to use executeAsModal when there is a possibility of changing the Photoshop display. Does that apply to this case as well and do I need to use it?

  2. According to the topic “Error: host is in a modal state”, if I put executeAsModal in the wrong place, it will interrupt with the operation of plugins created by others. Where is the right place to put it in this code?

export const App = () => {
  const defaultState = {
    selectedArtboard: null, 
  } ;

  const [state, dispatch] = useReducer(reducer, defaultState) ;

  // componentDidMount
  useEffect(() => {
    const onSelect = async (event, descriptor) => {
      if(descriptor._obj) {
        const layerInfo = await getLayerInfo(descriptor) ;
        if(layerInfo.artboard) {
          // update state based on artboard info
          dispatch( {type: ActionType.setSelectedArtboard, payload: {value: layerInfo}} ) ;
          return ;

      // update state with null
      dispatch( {type: ActionType.setSelectedArtboard, payload: {value: null}} ) ;
    } ;
    action.addNotificationListener(['select', 'selectNoLayers'], onSelect) ;

    // componentWillUnmount
    return () => {
      action.removeNotificationListener(['select', 'selectNoLayers'], onSelect) ;
    } ;
  }, []) ;

  return (
    <p>{state.selectedArtboard ? state.selectedArtboard.name : ''}</p>
  ) ;
} ;
  1. changing the panel UI is safe and can be done outside of modal state

  2. modifying the doc requires being in a modal state but you can read properties and events without modal just fine

that being said it does not matter where you put your code.

once PS enters modal state it refuses other plugins requests for modal until the current plugin finishes and you get that error message

@Maher Thanks for your answer. That matches my perception before reading “Error: host is in a modal state”.

I was concerned because getLayerInfo is what @AnthonyK calls a getter that runs in the background. If it can be done without executeAsModal, so much the better.