redux react-dom的钩子调用无效,我该怎么办?

5vf7fwbs  于 2023-08-05  发布在  React
关注(0)|答案(2)|浏览(126)

我想使用redux/toolkit创建一个全局模态组件并全局管理它。当我单击下拉菜单时,我创建了我想要显示模态的消息,当我单击“确定”或“取消”按钮和背景时,我尝试关闭它。
打开模态的事件没有问题,但要关闭它,请发送以下错误。附件是根据错误信息检查的内容。


的数据

  1. pacakge.json:
"react": "^18.2.0",
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.2.0",

字符串

  1. modalSlice.js
import { createSlice } from '@reduxjs/toolkit'

const initialState = {
  modalContent: '',
  isOpen: false,
}

export const modalSlice = createSlice({
  name: 'modal',
  initialState,
  reducers: {
    openModal: (state, actions) => {
      const { modalContent } = actions.payload
      const updateState = {
        ...state,
        isOpen: true,
        modalContent,
      }
      return updateState
    },
    closeModal: (state, actions) => {
      console.log(actions)
      const updateState = {
        ...initialState,
      }
      return updateState
    },
  },
})

export const { openModal, closeModal } = modalSlice.actions

export default modalSlice.reducer


Modal.js

const Modal = () => {
  const dispatch = useDispatch
  const { modalContent } = useSelector((state) => state.modal)
  const modalHandler = (e) => {
    dispatch(closeModal)
  }
  return (
    <>
      <ModalContainer>
        <ModalBack onClick={modalHandler} />
        <ModalContentWrapper>
          <ModalContent>{modalContent}</ModalContent>
          <ModalBtnWrapper>
            <ModalBtn onClick={modalHandler}>취소</ModalBtn>
            <ModalBtn onClick={modalHandler}>확인</ModalBtn>
          </ModalBtnWrapper>
        </ModalContentWrapper>
      </ModalContainer>
    </>
  )
}

  1. npm ls react
wedding_reception@0.1.0 /Users/mary/231014 wedding/mobile/wedding_reception
├─┬ @fortawesome/react-fontawesome@0.2.0
│ └── react@18.2.0 deduped
├─┬ @reduxjs/toolkit@1.9.5
│ └── react@18.2.0 deduped
├─┬ @testing-library/react@13.4.0
│ └── react@18.2.0 deduped
├─┬ react-copy-to-clipboard@5.1.0
│ └── react@18.2.0 deduped
├─┬ react-dom@18.2.0
│ └── react@18.2.0 deduped
├─┬ react-redux@8.1.1
│ ├── react@18.2.0 deduped
│ └─┬ use-sync-external-store@1.2.0
│   └── react@18.2.0 deduped
├─┬ react-router-dom@6.14.1
│ ├─┬ react-router@6.14.1
│ │ └── react@18.2.0 deduped
│ └── react@18.2.0 deduped
├─┬ react-scripts@5.0.1
│ └── react@18.2.0 deduped
├── react@18.2.0
└─┬ styled-components@6.0.2
  └── react@18.2.0 deduped

jecbmhm3

jecbmhm31#

无效的钩子调用警告您可能违反了钩子规则。

  • 就应该这样写 *
const dispatch = useDispatch();

字符串

zphenhs4

zphenhs42#

问题

起初,not 调用useDispatch会导致“invalid hook call”错误,这似乎很奇怪。在代码中,您保存了对useDispatch钩子函数的引用并将其命名为dispatch,然后在嵌套的回调函数中调用此函数,这显然违反了Rules of Hooks

只调用顶层钩子

不要在循环、条件或 * 嵌套函数 * 中调用Hooks。相反,在任何早期返回之前,始终在React函数的顶层使用Hooks。

const Modal = () => {
  const dispatch = useDispatch // dispatch is reference to useDispatch function

  const { modalContent } = useSelector((state) => state.modal)

  const modalHandler = (e) => {
    dispatch(closeModal) // e.g. "useDispatch" invoked here
  }

  return (
    ...
  )
}

字符串

溶液

在顶层正确调用useDispatch函数,以便 * 实际 * dispatch函数在回调函数中可调用。

const Modal = () => {
  const dispatch = useDispatch(); // dispatch is reference to dispatch function now

  const { modalContent } = useSelector((state) => state.modal);

  const modalHandler = (e) => {
    dispatch(closeModal); // e.g. real "dispatch" invoked here
  }

  return (
    ...
  )
}

相关问题