javascript - 如何在定义了返回流类型的化简器上使用 Object.assign 和文字
问题描述
我正在编写一个将与React.useReducer
钩子一起使用的减速器。使用了 Flow,但 reducer 遇到了流错误,因为Object.assign({}, state ...
has{}
作为与SearchLocationStateType
. 我发现的唯一解决方案是列出每个对象文字上的所有属性,这不是正确的方法。
我该如何解决这个问题?
type SearchLocationStateType = {
textAreaFocused: boolean,
promptsShown: boolean,
};
export const ACTIONS = Object.freeze({
FOCUS_SEARCH_TEXT: 'FOCUS-SEARCH-TEXT',
BLUR_SEARCH_TEXT: 'BLUR-SEARCH-TEXT',
});
type ActionType = {
type: $Values<typeof ACTIONS>,
};
const reducer = (state: SearchLocationStateType, action: ActionType): SearchLocationStateType => {
switch (action.type) {
case ACTIONS.FOCUS_SEARCH_TEXT:
return Object.assign({}, state, {textAreaFocused: true});
case ACTIONS.BLUR_SEARCH_TEXT:
return Object.assign({}, state, {textAreaFocused: false});
default:
return state;
}
};
解决方案
推荐阅读
- python - AttributeError:“NoneType”对象没有属性“保存”python-django
- python - 二叉树搜索未排序的矩阵?
- flutter - Completer Flutter hook 导致错误状态错误
- c# - 如何指定一个位置来为 Entity Framework Core 应用程序创建数据库文件?
- wso2 - 身份服务器 - 添加服务提供者空白页
- reactjs - 如何在 ReactJS 中链接动作?
- javascript - 可以使用一个模块来通信两个不同的 JavaScript 模块吗?
- javascript - 有没有办法编辑这个小部件在我的网站上的加载方式,以做出响应
- aws-lambda - (InvalidRequestException) 调用 GetQueryResults 时......从 Lambda Python 查询 Athena......无法读取结果
- c++ - 为什么 gcc 和 clang 为函数模板的实例化生成不同的符号名称?