最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable click events when execution is going on in React? - Stack Overflow

programmeradmin6浏览0评论

I have a screen

where I want to disable all the events when execution is going on.

When I click on the Execute button, an API is called which probably takes 4-5 minutes to respond. During that time, I don't want the user to click on the calendar cells or Month navigation arrows. In short, I want to disable all the events on the center screen but not the left menu.

Is it possible to do that?

I have a screen

where I want to disable all the events when execution is going on.

When I click on the Execute button, an API is called which probably takes 4-5 minutes to respond. During that time, I don't want the user to click on the calendar cells or Month navigation arrows. In short, I want to disable all the events on the center screen but not the left menu.

Is it possible to do that?

Share Improve this question asked Jan 11, 2021 at 12:53 shalu jshalu j 3571 gold badge5 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Yes sure, you can add a class with css pointer-events rule. Set it on the whole table and it will disable all events. Just add it when request starts and remove it when it ends. You can achieve that, by having a boolean variable isLoading in your state, or redux store and based on that add or remove the no-click class.

.no-click {
  pointer-events: none;
}

You can use classic loading overlay box over your content when some flag (i.e. loading) is true.

Other way to do it is to use pointer-event: none in CSS. Use same flag to set class to your content block.

Here is a working example in codesanbox: https://codesandbox.io/s/determined-dirac-fj0lv?file=/src/App.js

Here is code:

export default function App() {
  const [loadingState, setLoadingState] = useState(false);
  const [pointerEvent, setPointerEvent] = useState(false);

  return (
    <div className="App">
      <div className="sidebar">Sidebar</div>
      <div
        className={classnames("content", {
          "content-pointer-event-none": pointerEvent
        })}
      >
        <button onClick={() => setLoadingState(true)}>
          Show loading overlay
        </button>
        <button onClick={() => setPointerEvent(true)}>
          Set pointer event in css
        </button>

        {loadingState && <div className="loading-overlay"></div>}
      </div>
    </div>
  );
}
.App {
  font-family: sans-serif;
  text-align: center;
  display: flex;
  position: absolute;
  width: 100%;
  height: 100%;
}

.content {
  flex: 1;
  position: relative;
}

.loading-overlay {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
  z-index: 1;
}

.content-pointer-event-none {
  pointer-events: none;
}
发布评论

评论列表(0)

  1. 暂无评论