小编Shi*_*ari的帖子

警报在反应中出现两次

为什么这个警告框在一个简单的 useReducer 应用程序中出现两次,

单击此处 获取代码和框链接,

重新生成场景

  1. 使用向上箭头增加输入值(仅按一次)
  2. 通过向下键减少输入的值(仅按一次)

这是代码

import "./styles.css";
import { useReducer } from "react";
const reducerFunction = (state, action) => {
  switch (action.type) {
    case "testing": {
      if (action.payload.value > 0) {
        return { ...state, testing: action.payload.value };
      } else {
        alert("some message");
        return state;
      }
    }
    default: {
      throw new Error("Invalid Action");
    }
  }
};
export default function App() {
  const defaultValue = {
    testing: 0
  };
  const [state, dispatch] = useReducer(reducerFunction, defaultValue);

  return (
    <input …
Run Code Online (Sandbox Code Playgroud)

javascript alert reactjs react-hooks use-reducer

0
推荐指数
1
解决办法
1156
查看次数

标签 统计

alert ×1

javascript ×1

react-hooks ×1

reactjs ×1

use-reducer ×1