reactjs 打开devtool导致网页重新加载(Google Chrome)

mwecs4sa  于 2022-11-22  发布在  React
关注(0)|答案(1)|浏览(127)

我遇到了一个问题,我不确定问题是chrome,react还是macOs当我打开devtools时,我的组件重新渲染。我有关于redux-persistent的简单代码总结:当呈现Page 1时,如果用户关闭Page 2,我将尝试打开第二个浏览器,Page 1中的按钮将处于活动状态,以便在第二个选项卡上打开Page 2。当用户关闭/刷新Page 1选项卡时,Page 2选项卡也将关闭并清除persistent:root
我使用:Chrome浏览器:版本107.0.5304.110React:18.2.0 MacOS Monterey :12.3.1
代码示例:
App.js

import "./App.css";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Page1 from "./Page1";
import Page2 from "./Page2";
const App = () => {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Page1 />}></Route>
        <Route path="test" element={<Page2 />} />
      </Routes>
    </BrowserRouter>
  );
};

export default App;

Page1.js

import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { addItem, clearAll, createList, removeItem } from "./testReducer";
const Page1 = () => {
  const [isSecondScreenButtonDisable, setIsSecondScreenButtonDisable] =
    useState(false);
  const dispatch = useDispatch();
  const reducerList = useSelector((state) => state.counter);
  const array = [1];

  window.addEventListener("beforeunload", () => {
    localStorage.setItem("isSecondSreenOpen", "false");
    localStorage.removeItem("persist:root");
  });

  window.addEventListener("storage", () => {
    const isOpen = localStorage.getItem("isSecondSreenOpen");
    if (isOpen === "false") {
      setIsSecondScreenButtonDisable(false);
    } else {
      setIsSecondScreenButtonDisable(true);
    }
  });

  useEffect(() => {
    localStorage.setItem("isSecondSreenOpen", true);
    window.dispatchEvent(new Event("storage"));
    dispatch(createList(array));
  }, []);

  useEffect(() => {
    if (localStorage.getItem("isSecondSreenOpen") === "true") {
      window.open("test", "_blank");
    }
  }, []);

  return (
    <div className="App">
      <button
        disabled={isSecondScreenButtonDisable}
        onClick={() => {
          window.open("test", "_blank");
        }}
      >
        2. sayfa
      </button>
      <header className="App-header">
        <button onClick={() => dispatch(clearAll())}> Hepsini Sil</button>
        <button onClick={() => dispatch(addItem(100))}> Ekle</button>
        <button onClick={() => dispatch(removeItem())}> Sil</button>
        {reducerList.map((item, index) => {
          return (
            <div key={item + index}>
              {index}-{item}
            </div>
          );
        })}
      </header>
    </div>
  );
};

export default Page1;

第二页

import { useEffect } from "react";
import { useSelector } from "react-redux";
const Page2 = () => {
  const reducerList = useSelector((state) => state.counter);

  const onCloseEvent = () => {
    localStorage.setItem("isSecondSreenOpen", false);
    window.close();
  };

  window.addEventListener("beforeunload", () => {
    localStorage.setItem("isSecondSreenOpen", false);
  });

  useEffect(() => {
    localStorage.setItem("isSecondSreenOpen", true);
    window.addEventListener("storage", () => {
      const isOpen = localStorage.getItem("isSecondSreenOpen");
      if (isOpen === "false") {
        onCloseEvent();
      }
    });
  }, []);

  return (
    <div className="App">
      <header className="App-header">
        {reducerList.map((item, index) => {
          return (
            <div key={item}>
              {index}-{item}
            </div>
          );
        })}
      </header>
    </div>
  );
};

export default Page2;

store.js

import { combineReducers, configureStore } from "@reduxjs/toolkit";
import {
  persistReducer,
  FLUSH,
  REHYDRATE,
  PAUSE,
  PERSIST,
  PURGE,
  REGISTER,
} from "redux-persist";
import persistStore from "redux-persist/es/persistStore";
import storage from "redux-persist/lib/storage";
import { createStateSyncMiddleware } from "redux-state-sync";
// import  secondReducer  from "./secondReducer";
import testReducer from "./testReducer";

const persistConfig = {
  key: "root",
  version: 1,
  storage,
  // blacklist: ['secondReducer']
};

const rootReducer = combineReducers({
  counter: testReducer,
  // secondReducer: secondReducer,
});

const persistedReducer = persistReducer(persistConfig, rootReducer);



export const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
      },
    }).concat(
      createStateSyncMiddleware({
        blacklist: [PERSIST, REHYDRATE],
      })
    ),
});

export  const persistor = persistStore(store);

index.js

import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { Provider } from "react-redux";
import { persistor, store } from "./store";
import { PersistGate } from "redux-persist/lib/integration/react";
import { initMessageListener } from "redux-state-sync";

const root = ReactDOM.createRoot(document.getElementById("root"));
initMessageListener(store);
root.render(
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      <App />
    </PersistGate>
  </Provider>
);

reportWebVitals();

编辑:不仅我的组件重新呈现,它还从index.js开始

42fyovps

42fyovps1#

我发现答案不是关于React。我使用的扩展名为“真棒颜色选择器”当我禁用这个扩展的问题解决了。

相关问题