I'm developing an React JS
application that suppose to run on mobile devices.
Here is the code excerpts from the app:
index.tsx:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App/>
</React.StrictMode>,
document.getElementById('root')
);
App.tsx:
import React from 'react';
import './App.css';
import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom';
import {Header} from "./Components/header"
import {Footer} from "./Components/footer"
import {Sidebar} from "./Components/sidebar"
//...
const listener = () => {
console.log("Main listener")
};
function App() {
const media = window.matchMedia('(min-width: 700px)');
media.addEventListener("change", listener);
return (
<Router>
<div className="App">
<Header />
<Sidebar />
<Footer />
//...
</div>
</Router>
);
}
export default App;
The problem is that the change
event handler function (listener
) is called TWICE! when I resize the browser window. I've read investigated that the reason for that is the React Strict Mode
tag. As the official doc says the Strict Mode
makes double-invoking the following functions:
Class component constructor, render, and shouldComponentUpdate methods
Class component static getDerivedStateFromProps method
Function component bodies
State updater functions (the first argument to setState)
Functions passed to useState, useMemo, or useReducer
My question is WHY the binded event is called TWICE since the doc doesn't list the event handlers as being double invoked!?
It that context WHAT finally makes the change
event being invoked twice!?