- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
initialState 有错误:Argument of type '{ email: string;密码:字符串;有效: bool 值; }' 不可分配给 'never' 类型的参数。ts(2345)
function reducer(state: IState, action: IFluxAction) {
const Users = UsersFetch()
switch (action.type) {
case 'email':
const email = action.value
const valid = Users.find(e => e === email) ? true : false
return {
email,
valid,
}
case 'password':
const password = action.value
return {
password,
}
default:
throw new Error()
}
}
const initialState = {
email: '',
password: '',
valid: false,
}
const [state, dispatch] = React.useReducer(reducer, initialState)
输入此内容以满足错误的正确方法是什么?
react 16.8.1 typescript 3.3.1
应该(由...修复)将 ...state
添加到返回值中,例如
switch (action.type) {
case 'email':
const email = action.value
const valid = Users.find(e => e === email) ? true : false
return {
...state,
email,
valid,
}
case 'password':
const password = action.value
return {
...state,
password,
}
default:
throw new Error()
}
另外 - 正如@madflanderz 所建议的,将 IState 设置为 reducer 的预期返回值有助于捕获此类问题。
最佳答案
我也遇到过这个问题。在我看来,防止错误的最佳方法是将状态接口(interface)添加为 reducer 的返回类型。然后你会在 useReducer 行看到 reducer 内部的类型错误。
像这样:
function reducer(state: IState, action: IFluxAction): IState {
// reducer code
// type errors are visible here
}
关于reactjs - useReducer 的 initialState 类型为 never?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54771003/
为了深入研究 React Hooks,我决定尝试制作 snake 并使用 useReducer/useContext 状态管理。 现在,我被阻止在需要方向键来更改事件图 block 状态的地方。在 u
现在我尝试使用 useReducer 创建一种管理状态和功能的新方法,但现在发现问题是“Hooks 只能在函数组件的主体内部调用”有什么办法可以解决这个问题吗? // App Component im
我正在尝试创建一个可重复使用的useReducer与打字 Hook 。 这是我当前的代码: type State = { data?: T isLoading: boolean error
我正在编写一个自定义 Hook 来从 API 获取一些数据。如果可能的话,我希望返回的数据是类型安全的。这可以用泛型来完成吗? type Action = { type: 'PENDING' } |
我正在尝试创建一个可重复使用的useReducer与打字 Hook 。 这是我当前的代码: type State = { data?: T isLoading: boolean error
来自docs : [init, the 3d argument] lets you extract the logic for calculating the initial state outsid
我已经实现了一种相当简单的方法来将撤消添加到 useReducer 中,方法如下: export const stateMiddleware = reducerFunc => { return f
根据 React 文档: useReducer is usually preferable to useState when you have complex state logic that inv
我注意到在许多 useReducer 示例中,展开运算符在 reducer 中的使用方式如下: const reducer = (state, action) => { switch (actio
我想使用 React.useReducer 来更新状态。我的状态是一组对象。触发更新操作时,不仅更新所需索引中的值,而且更新所有这些值。我只想更新指定数组索引中的值。我该怎么做? 点击button1后
我有一个状态对象,其中包含一个名为 rows 的数组。该数组包含一个对象列表: {_id: "5e88ad4c5f6f7388d50b9480", stampa: "Confezione", S: 0
使用 useReducer 时是否可以使用调度功能发送多个操作? Hook react ?我尝试将一系列操作传递给它,但这会引发未处理的运行时异常。 明确地说,通常会有一个初始状态对象和一个 redu
我有这些: export type DataItemChild = { id: number; title:string; checked?: boolean; }; 请注意,子项可以在此
假设我实现了一个简单的全局加载状态,如下所示: // hooks/useLoading.js import React, { createContext, useContext, useReducer
我不仅需要在 ComponentDidMount 上使用 loadData() 从服务器获取数据,还需要在 onFiltersClear、onApplyFilters 和 方法之后使用>onPageC
如果我在使用 时需要添加一些副作用,我想知道我有哪些选择useReducer 钩。 例如,有一个 TODO-app: const initialState = {items: []}; const r
useReducer is usually preferable to useState when you have complex state logic that involves multipl
场景 我有一个返回操作的自定义 Hook 。 父组件“Container”利用自定义钩子(Hook)并将操作作为 prop 传递给子组件。 问题 当从子组件执行操作时,实际调度会发生两次。 现在,如果
我正在尝试使用新的 React useReducer API 获取一些数据,但卡在了我需要异步获取它的阶段。我只是不知道怎么办:/ 如何将数据获取放在 switch 语句中,或者这不是应该完成的方法?
这个问题在这里已经有了答案: Why is localStorage getting cleared whenever I refresh the page? (3 个答案) 关闭 5 个月前。 H
我是一名优秀的程序员,十分优秀!